How to Add PHP Code to WordPress Post or Page
access_time
hourglass_empty
person_outline

How to Add PHP Code to WordPress Post or Page

Looking to know how to run PHP code on a WordPress post or page? You’ve come to the right place. Here, we will uncover how to add PHP to a WordPress page or post so you can improve their functionality.

Reasons to Add PHP Code to WordPress Post or Page

By default, WordPress doesn’t allow you to run PHP code in posts or pages. The recommended way to add PHP functions is to modify the child theme or create custom page templates.

Despite so, there are occasions where you might want to add specific PHP functions to an individual post or page. In these cases, you can override the rule using WordPress code widget plugins.

The following tutorial will guide you through the process of using the Insert PHP Code Snippet plugin to add PHP code directly to a WordPress post or page.

How to Add PHP Code to WordPress Post or Page Using a Plugin

Before diving into the tutorial, make sure you have installed and activate the Insert PHP Code Snippet plugin. Check this tutorial for a thorough instruction on how to install and activate a WordPress plugin properly.

  1. From your WordPress dashboard, navigate to XYZ PHP Code -> PHPCode Snippets.
  2. In the PHP Code Snippets page, click Add New PHP Code Snippet. Add your desired PHP function and its tracking name to the appointed boxes. In this example, we will add the date() function that returns the local date and time. Then, click Create.
    This image shows you how to add PHP code to the Insert PHP Code Snippet plugin. This plugin will help you add PHP to WordPress post or page.
  3. If the process is successful, a confirmation message and the new function will appear on the screen. For future use, make sure all the PHP functions you want to use are active by checking the Action section. Active code snippets will show a green pause symbol while inactive ones will have a green checkmark next to them.
    This image shows you how to tell if the added function is active. To deactivate the function, click on the Pause button in the Action section..
  4. To add a PHP code to a WordPress post or page, paste the snippet shortcode of the function you want to use in the desired location. If you’re using the Gutenberg block editor, use the shortcode or code block to display it.
    This image shows you how to add PHP code in WordPress post using the shortcode and code block in Gutenberg editor.
  5. That’s it! Your newly created post should execute the PHP code like the following example:
    This image shows you how WordPress execute the PHP function you added using shortcode and code blocks.

Conclusion

Just because WordPress doesn’t allow you to run PHP code in a post or page, it’s not impossible to do so. Using WordPress code widget plugins, you can add various PHP functions to your posts or pages easily.

Do you find this tutorial helpful? Let us know in the comments section below!

The Author

Author

Domantas G. / @domantas

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.

Related tutorials

Author

okereke Reply

May 16, 2017

Thanks it was easy and great.....

Author

Naqi Reply

May 28, 2017

Thanks you very much i was searching for php code in wordpress like this

Author

Devon Reply

November 04, 2017

I did exactly as you say but this plugin doesn't work properly, it is crap.

    Author

    Domantas G.

    Replied on November 10, 2017

    Hey Devon, Why, do you get any errors? Provide more details, please.

      Author

      Eduardo

      Replied on December 19, 2017

      No errors, just doesn't work. No more details required.

Author

Dario Reply

November 08, 2017

Is it possible that it doesn't work for 'pages'?

    Author

    Alex

    Replied on November 21, 2017

    I did exactly as you say but it doesn't work. It shows this message: Parse error: syntax error, unexpected ‘&’ in /home/nt435/public_html/wp-content/plugins/insert-php/insert_php.php(48) : eval()’d code on line 3

Author

Fernando Reply

November 23, 2017

Thank you Guys

Author

bv nityanand reddy Reply

April 17, 2018

This plugin worked for me... had to deactivate some plugins before i got it to work

Author

Robson Reply

August 31, 2018

Thanks a lot!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in WordPress
How to fix the ERR_TOO_MANY_REDIRECTS on WordPress
How to fix err_too_many_redirects in WordPress

Close