There are many ways to “skin a cat” as they say, and this saying holds true for adding PHP code to WordPress pages and posts. There are plenty of ways to accomplish it. In my experience though, I have found that many of the suggested ways involves “disabling the WordPress visual editor.” That is, the WYSIWYG (what you see is what you get) editor. And well, I’d rather not.
Disabling the visual editor isn’t ideal for many reasons. I mean, if you are the only one writing and editing content that’s fine, but others may rely on it for fancying up their content. It makes it very simple to bold or italicize text. You can also add links and lists in a GUI sort of way. Not to mention, some plugins will have buttons that hold their shortcodes and the functionality for it may only be available to the visual editor.
Whatever the reason (or even if no reason at all that you can think of at the moment), to keep the editor, you don’t have to disable it anyway to execute PHP within posts and pages, so why bother? Good question. But how do we avoid disabling the editor? Another good question.
1) First of course, you want to download and install the plugin like you would any other.
2) Once it is activated, it is ready to use!
Opionally you can modify the default path where your PHP include files will reside. Remember the default is the current theme folder.
To change the path go to Settings >> Proper PHP Include and enter the new folder path into the text box. There is a path just above the folder that will give you an idea for the formatting.
3) Create and upload a PHP file.
4) In the edit screen for a post or page add the following shortcode:
Of course, replace filename.php with the name of your file.
5) Test the output to see if it works as desired.
PHP Code to Determine Your Age in Days
For fun, I will include the age calculation script below that I demonstrated in the video. It makes use of a “date_diff” function. This function is part of PHP 5.3 so the script is written in such a way that it will work even if the version of PHP that the code is being executed on isn’t 5.3 (not claiming it will work in all past and future versions of PHP though).
This PHP include file demonstrates a great use for the PHP Include plugin in that it allows you to inject “dynamic” components into a post or page. In this example the “age in days” will change in the post as time passes, without the need to go back to the post and modify it manually every day.
Now, it would be nice if (or it is nice when) theme or site developers would do this with the copyright year in the footer of web sites. It makes a site look outdated when the copyright year has long since past, wouldn’t you agree? It requires just one quick PHP command to make it dynamic. I know so many people with PHP driven sites that have it on their yearly to-do list to update the copyright year.
I digress, here’s the code used in the Proper PHP Include sample in the video. Use at your own risk (it might shock you how many days old you are!).