Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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:

  1. Search for "Insert PHP"

  2. Click the plugin's "Install Now" link.

To upload to the /plugins directory:

  1. Download Insert PHP from either the WordPress.org plugin page or directly from this link.

  2. Unzip the downloaded file.

  3. 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.

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC