How to Run PHP under WordPress with Justyn’s Magic Includer

Were you to call a gardener to come round and tidy the garden, you’d give them instructions such as “mow the lawn, dig out weed clumps, then use my long-handled shears to trim grass overhanging the edges”. These seem clear, but they’ve omitted something crucial. Where should the gardener find the mower, the trowel, and the shears? In their van; in your shed; or somewhere undefined amongst bits of DIY kit in the garage? I faced a similar situation when writing the script described in “How to Bulk-add Gigs in an AudioTheme Theme”. I solved it with Justyn’s Magic Includer, a.k.a. The Ultimate Include/Require Script for WordPress by Justyn Hornor.

The analogue to tools in a program is “functions”: self-contained pieces of code that perform an action or return data, possibly the result of a calculation. In my script, these include classes(), which returns the names, times, days and venues for the teacher’s classes; terms(), which returns the three parts of each year when these classes take place; is_in_term(), which works out whether any given date falls within a specified term; and add_class(), which adds all the classes of a given type to the gig listing. These are tools I’ve made.

But they depend on other people’s tools. My function add_class() wouldn’t work unless it could call the functions written by AudioTheme for inserting gigs. And because gigs are represented as posts, those wouldn’t work unless they could call the functions written by WordPress’s authors for handling posts. So when I’m running my programs, I need them to know that AudioTheme’s functions are in the shed over there, whereas the WordPress functions are buried at the back of my garage behind that pile of motorbike spares. This is what Justyn’s Magic Includer does. It enables code to run and have access to all the WordPress functions, even though I’m running it at a terminal rather than by clicking WordPress buttons or menus.

To use it, download his script or copy the one below. Then just load it into PHP before the other code you want to run.

<?php
/* Justyn's Magic Includer */
/* Slightly modified by Jocelyn Ireson-Paine */

/*
This is a script I use for loading the 
WordPress files. It's from "The Ultimate 
Include/Require Script for WordPress", by 
Justyn Hornor,
www.sitepoint.com/the-ultimate-includerequire-script-for-wordpress/ 

I have changed the formatting to the style I
prefer. At the end of the file, there was 
some code for generating a table of admin
users, as a test that the script is working.
I've hived that off into a separate function,
justyn_test(). You can call that as a test
after including the script.

The following comments are Justyn's. Assumes 
you are using the root folder of your server 
for your WordPress install. If not, just add 
the sub-folder where for WordPress you are using.

For example, add the sub-folder to the $location 
variable:
$location = $_SERVER['DOCUMENT_ROOT'] . '/your-sub-folder';
*/

$location = '/home/jenny/www/www.danceanddare.co.uk';

include( $location . '/wp-config.php' );
include( $location . '/wp-load.php' );
include( $location . '/wp-includes/pluggable.php' );

global $wpdb;

if ( !isset( $wpdb ) ) {
  include( $location . '/wp-config.php' );
  include( $location . '/wp-includes/wp-db.php' );
}


/* A function to test the above include 
   statements are working by listing all 
   the admins and their email addresses in 
   a table.
*/
function justyn_test()
{
  $args = array( 'role' => 'Administrator'
               , 'fields' => 'all_with_meta'
               );

  $query = get_users( $args ); 
  // Use the get_users call.

  echo '<table cellpadding="0" cellspacing="0" border="1">' . "\n";
  echo '<tr><thead><th>Nice Name</th><th>Email</th></thead></tr>' . "\n";
  // Start the table.

  foreach ( $query as $query ) {
    echo '<tr><td>' . $query->user_nicename . '</td><td><a href="mailto:' . $query->user_email . '">' . $query->user_email . '</a></td></tr>' . "\n";
  } 
  // For each of the admins, make a table row.

  echo "</table>\n"; 
  // Close the table.
}

?>

Here’s a demo. I ran it interactively, but my interactive PHP turns out to be the so-called “Interactive Mode” rather than “Interactive Shell”. This is explained at “Interactive shell ¶” on php.net . It means that even though I’m running the code from the terminal, PHP doesn’t execute the commands until I type an end-of-file. So below, I include Justyn’s Magic Includer and then run his test. This generates a table of the admin users and their email addresses, but doesn’t do so until I type end-of-file, shown as ^D.

jenny@onza:~/www/www.danceanddare.co.uk$ php -a
Interactive mode enabled

php > include('justyn.php');
php > justyn_test();
php > ^D<table cellpadding="0" cellspacing="0" border="1">
<tr><thead><th>Nice Name</th><th>Email</th></thead></tr>
<tr><td>Jocelyn</td><td><a href="mailto:jocelyn@supermail.com">jocelyn@supermail.com</a></td></tr>
<tr><td>Jenny</td><td><a href="mailto:jenny@supermail.com">mailto:jenny@supermail.com</a></td></tr>
</table>
jenny@onza:~/www/www.danceanddare.co.uk$ 

And here’s the table as rendered by your browser:

Nice NameEmail
Jocelynmailto:jocelyn@supermail.com
Jennymailto:jenny@supermail.com

Leave a Reply

Your email address will not be published. Required fields are marked *