How to Insert PHP Code in a WordPress Post

Every now and then you require running PHP functions inside WordPress page or post. For this, you have to insert PHP Code in your WordPress post or page.

However, sadly, though, by default, WordPress does not execute PHP Code within a blog post as it does not recognize PHP programming code.

So, what’s the way out for you?

Well, you have as much as two methods of running any kind of custom PHP Code within WordPress posts and pages.

Firstly, you can build a shortcode for running PHP Code by inserting it into the post content.

Secondly, you can use Plugins which enable you to use PHP within WordPress posts and pages.

Here, I am going to illustrate both of these methods so that you can easily insert PHP Code in a WordPress Post.

Custom Shortcode Method

To illustrate this method, I will be building the custom shortcode for outputting a complete list of existing WordPress pages.

For this, the PHP function is – “wp_list_pages ()”.

Now, open the functions.php file and paste the following code before the closing ?> PHP Tag.

function list_pages_function( $atts ) {
return wp_list_pages(‘sort_column=menu_order’);
}
add_shortcode (‘output_pages’, ‘list_pages_function’);

Here, you should note that a shortcode has been created by the name “output_pages”. You can see that the first parameter of the add_shortcode function is “output_pages”.

The second parameter is “list_pages_function” which directs WordPress to perform the contents of this custom function when it finds the particular shortcode.

This will enable to execute the PHP within the function that you have created. When you save the functions.php file, the shortcode has been created.

Using the Shortcode

Taking the above example, the name of the shortcode is “output_pages.” Now, open any WordPress post in the editor and type the name of the shortcode using square brackets such as [output_pages].

It will execute the code in the custom function.

As for this example, it simply lists all the pages on the site.

Using Plugins to run PHP Code inside WordPress Posts or Pages

Insert PHP Plugin

The Insert PHP Plugin enables you to run PHP Code in WordPress Posts and Pages. It is simple to use. Using this plugin, you can do pretty much that PHP can do when it is embedded in the PHP Templates. The plugin code is short for quick loading and running.

You simply have to paste the PHP Code into the post or page and then replace <?php and ?> in the PHP Code with [insert_php] and [/insert_php].

Here is an example:
<?php
echo “Hello Friend!”;
echo “How are you :)”;
?>

Change this to:

[insert_php]
echo “Hello Friend!”;
echo “How are you :)”;
[/insert_php]

It is simple as that.

You can install Insert PHP from the WordPress Dashboard, or you can upload it to the plugins directory.

Insert PHP Code in a WordPress Post

Insert PHP Code Snippet

The Insert PHP Code Snippet Plugin enables you to add your PHP code to WordPress posts and pages without the need for custom templates.

The plugin provides a shortcode and options page so that the code can be added to the admin options page and then output in the posts using shortcodes.

Allow PHP in Posts and Pages Plugin

Using “Allow PHP in Posts and Pages” WordPress Plugin you can add the PHP code to the HTML post editor and run the PHP codes which were earlier blocked due to non-recognition of PHP programming code.

By adding a shortcode [php], your code [/php] “Allow PHP in Posts and Pages” Plugin includes PHP in the WordPress post and pages. With the use of this plugin, WordPress tags automatically disable <p> and <br/> and you are allowed to add your tags with the BBcode items such as [p] [/p] and [br/]. It also allows you to save frequently used bits of code in the form of snippets to be used across various pages.

It is simple to install and use Allow PHP in Posts and Pages Plugin.

You just need to login to your WordPress admin panel. Then in the main menu click “Plugins” and next click “Add New.” Type in the search box “Allow PHP in Posts and Pages” and click on “Search Plugins” button. Once the search is over, find the plugin in the results area and click the install link featured below its title. When prompted click on the “Activate Plugin.”

Further, click on the “Posts” and “Add New.” In the editing screen click the “HTML” tag and type the function code. Then click “Update” or “Publish.” Finally, you can check your work by clicking on “View Post.” The function then executes from the post itself.

PHP Code in a WordPress Post

So, you see that it is easy to insert PHP Code in a WordPress Post or Page. You can do it by using either shortcode method or by using plugins. Both methods are simple and straightforward.

2 thoughts on “How to Insert PHP Code in a WordPress Post”

Leave a Comment

Your email address will not be published. Required fields are marked *