Insert PHP WordPress Plugin

Instructions- Insert PHP WordPress Plugin

Insert PHP description page

Using Insert PHP is easy. You’ll understand when you see the examples.

This page contains installation instructions, plugin use instructions, examples, and how to deal with PHP error messages.

For convenience, the menu below is linked to specific sections on this page.

Installation

To install Insert PHP, either use the WordPress Dashboard or upload the plugin to the /plugins directory.

To use 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:

After Insert PHP has been installed via the dashboard or uploaded into the /plugins directory, activate the plugin.

Instructions for use and examples are below.

Quick Debug

When something’s wrong and you don’t know what, these three steps might solve it.

  1. Verify you’re using the «Text» tab and not the «Visual» tab when inserting or editing the PHP code.
  2. Verify your PHP code doesn’t throw an error. Error logs are your friends.
  3. Have a look at the Code Elements That Won’t Work section of this page.

The issue or error may be something else. But the above might find it for you without a lot or long effort.

Other sections of this page might also throw light on what you’re experiencing.

Insert PHP Instructions and Examples

Using Insert PHP is easy.

Take your working PHP code and replace the beginning «<?php» and the ending «?>» with «[insert_php]» and «[/insert_php]», respectively.

As an example, this:

<?php
echo 'Server date and time is: ';
echo date('l, F j, Y \a\t G:i:s');
?>

Becomes this:

[insert_php]
echo 'Server date and time is: ';
echo date('l, F j, Y \a\t G:i:s');
[/insert_php]

Paste the PHP code with the new beginning and ending into the post or page at the point where it shall run.

When the PHP code is run, the code itself will be removed from the post or page. If the PHP code generates output (like the above example, which publishes a date), the output will be published where the PHP code was.

More Examples

In the following examples, the beginning «<?php» and ending «?>» have already been replaced with with «[insert_php]» and «[/insert_php]».

  1. Publishing the local time.

    The first example, in the previous section, publishes the server time. If the server’s clock has a time zone different than your local time, the time needs to be adjusted.

    We’ll use Greenwich Mean Time from which to determine the local time. GMT is often used as a universal time zone for this purpose.

    (By the way, site user’s computer clocks can’t be depended on to be correct. JavaScript that consults the user’s computer time for its calculations. It’s why PHP and the server time is used instead of JavaScript.)

    To adjust your time one hour later than GMT, use

    [insert_php]
    echo 'Local date and time is: ';
    $hours = 1;
    echo gmdate('l, F j, Y \a\t G:i:s', time()+intval($hours*60*60));
    [/insert_php]
    
    

    For six hours earlier the GMT, use

    [insert_php]
    echo 'Local date and time is: ';
    $hours = -6;
    echo gmdate('l, F j, Y \a\t G:i:s', time()+intval($hours*60*60));
    [/insert_php]
    
    

    The only difference in the two above instances is the value of the $hours variable. To specify a later time, use a positive number; to specify an earlier time, use a negative number.

    A decimal number may be used for the value of $hours. (Some time zones adjust by ½ hour instead of full hours.)

  2. Inserting the content of a file located on the server of your domain.

    The content of a file can be inserted into a post or page.

    You’ll need to know the location of the file. The location would be the URL minus the http and domain name part. As an example, if the file is at URL «http://example.com/filename.txt» then the location is «/filename.txt»

    [insert_php]
    $FileLocation = "/filename.txt";
    readfile($_SERVER['DOCUMENT_ROOT'].$FileLocation);
    [/insert_php]
    
    

    Note: Inserting an image file won’t work as expected. The code to construct the visual image is the content of the image file. The code will be published, not the visual image itself. To insert a visual image, see the «Inserting a visual image» example further below.

  3. Inserting the response from a URL somewhere on the internet.

    This will work on some servers and not on others. It depends on whether or not the feature is enabled for your domain, which is up to the hosting company (technical information).

    You can ask the hosting company if you can use URLs in the PHP readfile() function. Or just give it a try and see if it works.

    Specify the URL of the content for the $URL value.

    [insert_php]
    $URL = "http://example.com/filename.txt";
    readfile($URL);
    [/insert_php]
    
    

    This method can be used to insert the output of Perl CGI and other scripts.

    [insert_php]
    $URL = "http://example.com/cgi-bin/current_temperature.cgi";
    readfile($URL);
    [/insert_php]
    
    
  4. Inserting a visual image.

    Specify the URL of the image for the $URL value.

    The $ImageWidth and $ImageHeight values are optional. They can be specified as 0 (zero), or one or both can be assigned a positive number. The number is the pixel width or height of the published image. The number does not have to correspond to the actual image size; just specify the size it shall publish as.

    Optionally, you may also specify an alt tag value.

    [insert_php]
    $URL = "http://example.com/images/picture.jpg";
    $ImageWidth = 200;
    $ImageHeight = 0;
    $AltTag = "";
    /* end of customization section */
    $alt = empty($AltTag) ? "" : " alt='$AltTag'";
    $width = (!empty($ImageWidth) and $ImageWidth>0) ? "width:${ImageWidth}px;" : "";
    $height = (!empty($ImageHeight) and $ImageHeight>0) ? "height:${ImageHeight}px;" : "";
    $css = ($width or $height) ? " style='$width$height'" : "";
    echo "<img src='$URL'$css$alt />";
    [/insert_php]
    
    
  5. Inserting other PHP code.

    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 at the file location. 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. Click «Preview Changes» instead of «Update.»

    If it works in preview, it works. Otherwise, it doesn’t and the code can be removed from the page.

  6. Redirecting if a certain cookie is missing.

    Redirecting with PHP can not be done because by the time this PHP code is run, the headers will already have been sent. However, JavaScript can be used to redirect if a certain cookie is missing.

    [insert_php]
    $CookieName = "TestCookie";
    $RedirectURL = "http://example.com/page.html";
    if( empty($_COOKIE[$CookieName]) ) { echo "<script type='text/javascript'>location.href='$RedirectURL'</script>"; }
    [/insert_php]
    
    

Important note: The PHP code used with this Insert PHP plugin can not set a cookie or specify any other headers, including redirects. By the time the PHP code is run in the post or page, the header lines will all have been sent already. Trying to set a cookie or other header lines here will result in an error message.

Code Elements That Won’t Work

The header() function won’t work because headers have already been sent before the Insert PHP code is run. After headers are sent and the web page code has been started, no new headers can be set.

The setcookie() function won’t work because PHP sets cookies in the hea