Run PHP code in WordPress in posts and page using shortcodes


To totally unlock this section you need to Log-in


Login

WordPress is a popular CMS and blogging software that, natively, doesn't have the ability, by default, to run PHP code writter directly in posts and pages (this is basically for security reasons). But there are some plugins that enable WordPress to do that throught secure shortcodes (short text strings that usually call PHP code, not only, when a page is requested by client to the server).

Run PHP code in WordPress in posts and page using shortcodes

Run PHP code in WordPress in posts and page using shortcodes

NOTE: you can download these two plugins directly from this article going to the Download section (the two plugins are the same located on WordPress.org, they're moved here just for a mirror/availability reason).

Two powerful plugins that enable WordPress to handle PHP in posts and pages (even in widgets) are available:

1. - Insert PHP

2. - Shortcode Exec PHP

Shortcode Exec PHP

Shortcode Exec PHP can be used to execute PHP code in Posts, Pages, Widgets, Comments and RSS Feeds. From the plugin setting page PHP codes are added and corresponding custom shortcodes are generated.

This plugin has an easy administration section in which the administrator can define shortcode's name, its code writing it in the easy-to-use editor provided by the plugin, and test it directly, without exiting from the administration page.

Insert PHP

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 (Add New link) or uploaded to the /plugins directory, in your WordPress root directory.

To install from the WordPress Dashboard:

At the "Plugins - Add New" dashboard menu item search for "Insert PHP", then click the plugin's "Install Now" link.

If this procedure, for some reason, doesn't work you can upload it to the /plugins directory:

Download Insert PHP from either the WordPress.org plugin page or directly from this link and then unzip the downloaded file.

Upload the unzipped "insert-php" directory to the WordPress /plugins directory.

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.

Download

To download the attachments of this article you'll need to be registered and logged on. Sorry for the inconveniencce.
[wpfilebase tag="file" id=147 /]
[wpfilebase tag="file" id=148 /]