WordPress is great for creating web pages! HTML is cool, but, from time to time, or on a more regular basis, you need to add some PHP code to your page. Let’s take a crash course on the subject of inserting custom PHP code directly in your posts and/or pages!
If you need a custom php code executed in your post or page, you probably tried doing something similar as I did – copy pasted it directly into the editor. Next, you wondered why isn’t it working. Well, the content you put in the editor is either:
- Text, image, video, audio or any feature supported by the Visual editor.
- HTML code in the HTML editor mode
And that’s why it won’t work. By default WordPress excepts HTML code, not PHP.
Adding PHP Code to Your Post Using a Plug-in
WordPress is built in PHP, so it’s logical that it can use it, but for our purpose we will need to add a plugin that actually tells WordPress that it needs to execute PHP code. Type “Inline PHP” in the plugins search field (Plugins/Add New) and install and activate the “Inline PHP” plugin. You can also use the “PHP Shortcode” plugin that is based on the previously mentioned one.
Once you install one of these plugins adding PHP code is very simple. You simply use a shortcode to tell WordPress that there is a php block there!
Using the Inline PHP plugin:
[ exec] echo 'This is a test'; [ /exec]
Using the PHP Shortcode Plugin
[ php]
$a = 1;
$b = 2;
echo $a + $b;
[ /php]
Note: I added a space after the “[" so the plugin does not kick in. If I would not, you would not see the actual code. So, when you use the shotcodes, remove the space before the exec and /exec (or php /php if you installed the PHP Shortcode plugin).
WordPress Coding Usage Scenarios
And now you can go code-happy and add all kinds of PHP codes to your pages!
For example, you can write a page (not a post) about one of the subjects on your blog or website and list all the articles in that category using a simple php call.
You can also use php in widgets to add some custom gizmos you created on your own.
If your PHP code is quite extensive, you can create your own function that can be called with a single PHP line so your post does not get "overloaded with code". To do this you need to do the following:
- Create the actual PHP function and add it to a folder somewhere on your site
- Include the function in the theme's functions.php by adding this in the file:
include_once (/my_custom_php_code/my_custom_function.php');
With this you made your function(s) recognizable to WordPress. Note that the path is relative from the root folder of your website. - Execute the PHP function using the shortcodes in your posts, pages or sidebar widgets. For instance if you have a function in the included .php that is called DisplayDate() you would call it using:
[ exec]DisplayDate();[ /exec]
Again, I added spaces so the code does not get actually executed.
And that’s it! Happy PHP WordPress coding!





Yeah, that’s cool.
You can also extract the PHP to it’s own file and process shortcodes, which is what I suspect the plugin is doing. It’s probably worth examining that plugin it detail to see how it works. At some point it has to turn the string delivered by the short code into a function call(s).
Dave Doolin recently posted..WIAW! Bolder and Brighter with a Cheerful New Design
Thanks, i was wondering how to use functions in my posts (can’t redeclare if the function is defined in the post).
Masamune recently posted..Starter kit de Référencement – Les bases
Awesome post and, this is some thing I really need, Keep posting
After all, i think the best way to include lot of PHP in wordpress pages is to do it directly in template page.
Plugins are juste good for small casual amounts of code, or use in widgets ^^
Masamune recently posted..Hard Cops Uprising OST – Musique