How to Insert PHP Code in a WordPress Post

How to Insert PHP Code in a WordPress Post

By default, WordPress does not execute PHP code within a blog post. While you can use Javascript in the “Text” tab, there’s no way to introduce conditional statements or use complex logic to display dynamic content. Say for example I want to output a complete list of existing pages within a WordPress post. The standard implementation of WordPress doesn’t allow this to happen short of manually typing in the various pages and their links. But this is static – it won’t update automatically when new ones are added.

If you try and use PHP code within a WordPress post using the code editor, it simply will not render. Basically, if you try something like this:

this will not work

You’ll just get a blank output in the final display.

There are many plug-ins that give us the capability of using PHP within posts and pages. But if you’re like me, you want to avoid using plug-ins whenever you can. In this tutorial, I’m going to show you a simple example of how to build a shortcode that can be inserted into post content and can run any kind of custom PHP code.

Building the Custom Shortcode

In this example, I’m going to use code to output a complete list of existing WordPress pages. The PHP function to do that is: “wp_list_pages()”. To implement this, I open up my functions.php file and paste in the following 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' );

See my earlier tutorial on how to insert custom code into WordPress if you’re not sure of where exactly to place this.

There are several important points to note in this little code snippet. The first is that I’m creating a shortcode by name “output_pages”, which is the first parameter of the “add_shortcode” function. You should change this to something more suitable for you depending on what you’re trying to accomplish. The second parameter “list_pages_function” tells WordPress to execute the contents of this custom function when it encounters the specified shortcode.

Finally, I can execute the actual PHP within the function I created. When I save my functions.php file, the shortcode has been created.

Using the Shortcode

Using the new shortcode couldn’t be easier. In our example, the name is “output_pages”. So open up any WordPress post or page in the editor and type in the name of your shortcodes within square brackets: [output_pages].

using the shortcode

This will execute the code in our custom function which in this case simply lists all the pages on the site:

list of pagesj

Adding Parameters and Content

If you want to change the way the shortcode renders, you can add parameters as well as content to it. For example, you can enclose content between the opening and closing tags of your short code like this:

[output_pages]Test Content[/output_pages]

When you execute your PHP code, you can capture the content between the tags inside the $content variable which is passed to your custom function if you choose to accept it. So the code above would change to be this instead:

function list_pages_function( $atts, $content ) {
// Do something with the $content variable
return wp_list_pages('sort_column=menu_order');
}
add_shortcode( 'output_pages', 'list_pages_function' );

Note the new addition in bold. You can even take it one step further and parse parameters within the shortcode which gets stored within the $atts array. In most implementations, it’s common to use a PHP function to specify default values for the parameters and extract them into individual variables for use. Those are more advanced implementations.

But if you simply want a quick and easy way to insert reusable (or standalone) PHP content into your post content without having to install an extra plug-in, basic shortcodes are the solution to your problems.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.