How to get your command line PHP script working with WordPress-MU 2.7

OK, this one is obscure. I only post about what I can’t find on Google!

Until recently it has been possible to run command-line PHP scripts that include wp-config.php or wpmu-settings.php, in order to give those scripts access to WordPress globals like $wpdb, functions such as wp_insert_post etc.

These scripts worked with WordPress-MU until recently — now the scripts just exit with no output.

The reason? WPMU now initiates a redirect in the bootstrap process (search for ‘header( “Location: “‘ in wpmu-settings.php). A browser can follow that redirection, but a command line script cannot. So, if you want to initiate scripts that run from the command line, you will have to get the web server involved by invoking a text browser such as curl, wget, or links.

It’s best not to force your scripts to figure out where to find wp-config.php anyway. I have taken to using a method suggested in this thread, as shown in the code sample below. The good news is, you know that your script will work the same whether initiated from a plugin in a graphic browser, used in an Ajax call, or initiated from cron or the command line shell.

So instead of kicking your script off with something like

/usr/bin/php -q myscript.php

You will create the script as a proper plugin and do something like

/usr/bin/curl -d mypp_cmd=status

Sample plugin code used for the above example follows. The prefix for the functions is ‘mypp’, which of course stands for ‘MY Plugin Prefix’ to create a unique namespace. The code below returns results as JSON encoded, and calls die()/exit() at the end to prevent an entire page being created. For log files run from cron, you may choose to return plain text instead.

   * Add a query var for this plugin
   * This allows Ajax programs to operate without requiring file paths
   * Instead, Ajax functions look for the query var 'mypp_cmd'
  function mypp_query_vars($qvars) {
    $qvars[] = 'mypp_cmd';
    return $qvars;
  } // function mypp_query_vars

   * Handle AJAX requests in the template_redirect action
   * We recognize our requests from the query var 'mypp_cmd'
   * Here we allow GET requests using $_REQUEST; to restrict to POST, use $_POST instead
  function mypp_template_redirect() {
    global $wpdb;
    $cmd = get_query_var('mypp_cmd');
    $response = array();
    if ($cmd) {
      switch($cmd) {
      case 'status':
	$response['status'] = array('success' => true,
				    'message' => 'Sample status message');
	$response['status'] = array('success' => false,
				    'msg' => 'Unknown command',
				    'cmd' => $cmd);
      } // switch $cmd
      header('Content-type: text/x-json; charset=utf-8');
      print utf8_encode(json_encode($response));
    } // $cmd is set
  } // function mypp_template_redirect

add_filter('query_vars', array($this, 'mypp_query_vars'));
add_action('template_redirect', array($this, 'mypp_template_redirect'));

Tagged with: , , , , , ,
3 comments on “How to get your command line PHP script working with WordPress-MU 2.7
  1. James says:

    Even easier than this is to set $_SERVER[‘HTTP_HOST’] to the correct domain before loading wp-load.php – then your PHP cli scripts work as before.

  2. Greg, this was very useful. I was calling a script in the way you described and having the same problem. Searched for a good while before coming here and now it works. Thanks

  3. Richard says:

    I found the following to work on WordPress 3.01:

    //WP Functions that I’m overriding
    //Don’t Redirect
    function auth_redirect(){};

    //Pretend to be a real user
    function wp_get_current_user() {
    global $current_user;

    return $current_user;

    $_SERVER[‘PHP_SELF’] = ‘/wp-admin/admin.php’;
    $_SERVER[‘REQUEST_URI’] = ‘/wp-admin/admin.php?page=page_to_fake’;
    $_SERVER[‘HTTP_HOST’] = ‘domain’;

    //Path to the admin file

Leave a Reply

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