Run PHP Code in WordPress Posts and Pages
The Insert PHP plugin runs PHP code within individual WordPress posts and pages. Simple to use, too.
With this plugin, do pretty much anything PHP could do if it was embedded in the PHP templates. Examples are further below.
The plugin code itself is short, less than 30 lines and all in one file for quick loading and running.
Using Insert PHP
To use, paste the PHP code into the post or page and then:
Replace <?php and ?> in your PHP code with [insert_php] and [/insert_php]
As an example, change this PHP code
<?php echo "Hello, World!"; echo "You are my friend :)"; ?>
to this
[insert_php] echo "Hello, World!"; echo "You are my friend :)"; [/insert_php]
and you're good to go. It can be that simple.
Installing Insert PHP
Insert PHP can be installed from the WordPress Dashboard or uploaded to the /plugins directory.
To install from the WordPress Dashboard:
At the "Plugins - Add New" dashboard menu item:
Search for "Insert PHP"
Click the plugin's "Install Now" link.
To upload to the /plugins directory:
Download Insert PHP from either the WordPress.org plugin page or directly from this link.
Unzip the downloaded file.
Upload the unzipped "insert-php" directory to the WordPress /plugins directory.
When Installed or Uploaded:
After Insert PHP has been installed via the dashboard or uploaded into the /plugins directory, activate the plugin.
Instructions and Examples:
See the Instructions and Examples page for basic instructions, half a dozen examples, and what to do if you get an error message. More examples and ideas for using Insert PHP follow.
Ideas and Examples for Using Insert PHP
Here are two ideas for things that can be done with Insert PHP. Following these, you'll find links to some others.
Local Time with PHP
JavaScript time functions are good. But they depend on the computer's clock for their time and time zone information. Computers users may not have the correct time set on their computer or even the correct time zone.
To determine the time at a specific time zone, using PHP is better than using JavaScript.
In the PHP code below, change the number following the $hours variable to indicate how many hours the local time zone is from Greenwich Mean Time (GMT). A decimal number may be specified.
For local time at GMT, specify 0. For a later time zone, specify a positive number. For an earlier time zone, specify a negative number.
This example publishes the current local time at the city of Chicago, USA:
[insert_php] $hours = -6; echo 'Local time and date is: '; $adjusted = time()+intval($hours*60*60); echo gmdate('G:i:s - F j, Y', $adjusted); [/insert_php]
Including PHP Code From an External File
PHP code from an external file can be included and run within a post or page using the Insert PHP plugin.
To insert and run other PHP code, you'll need to know the file's location. The location would be the URL to the file minus the http and domain name part.
As an example, if the file is at URL "http://example.com/scripts/mycode.php" then the location is "/scripts/mycode.php"
[insert_php] $FileLocation = "/scripts/mycode.php"; include($_SERVER['DOCUMENT_ROOT'].$FileLocation); [/insert_php]
The above will include the file specified for the $FileLocation variable.
The file needs to be a valid PHP file;. Otherwise, it will spawn an error message.
Note: If the included file uses MySQL, there may be conflict between it and WordPress' use of MySQL. The included file can be updated to not be conflicting, if needed. Depending on the file, it may be a quick or an involved update.
The conflict is the same as it would be if the PHP code was put directly into a WordPress template. It comes from another program's code merged into and running in conjunction with WordPress, both using the MySQL database at the same time and one of them failing to specify which open handle to use.
If unsure, test it. Paste the code into a post or page. Then, click "Preview Changes" instead of clicking "Update."
If it works in preview, it works. Otherwise, it doesn't and the code can be removed from the post or page.
Other Ideas and Examples
Here is a list of ideas and examples that work with Insert PHP.
-
Six examples are at the Instructions and Examples page.
-
Random content delivery can be done as describe at Random Content With PHP.
-
Download Handler works well with the Insert PHP plugin. In one central control panel, keep track of all file downloads from your website. Statistics by date, by date range, and by traffic source.
-
The Importing Any Content From an External File Using PHP article describes how to, well, import content from an external file.
That will get you started.
By the time you've reviewed the above and, perhaps, tried a few, you'll begin to understand the potential of the Insert PHP plugin. Running PHP code within individual WordPress posts and pages makes a lot of functionality available that isn't available otherwise.
Will Bontrager