Controlling WordPress plugins remotely

One of the important reasons for the popularity of WordPress is its plugin architecture, which has enabled thousands of programmers to write additional functionality for WordPress. But the plugins are constrained in silos, unable to be controlled by outside programs ( well this can be advantageous if you consider security).

Take the popular WP-Super-Cache plugin. I frequently make changes to my blog from a FTP client, and every time I’ve to login to the WordPress admin and clear the cache so that I can see the latest changes reflected. At times this can be tiring. It would be nice if one could have a desktop app that interfaces with the remote WP-Super-Cache instance. This is exactly what we will do in the current post.

RPC interface plugin

WordPress provides the XML_RPC interface to remotely post content to your blog. By default this interface is disabled for security reasons. But it can be a wonderful way to communicate with your WordPress plugins. WordPress has an extensive set of actions and filters which allow you add/change/remove low level features via plugins and themes. Using this technique plugins can expose new XML-RPC methods for your WordPress blog. A skeleton code to do the same is given below.

add_filter('xmlrpc_methods', 'my_xmlrpc_methods');
 
function my_xmlrpc_methods($methods) {
	$methods['new_rpc_method'] = 'my_method';
	return $methods;
}
 
function my_method($args)
{
   // Do all your custom programming here
}

The following is a short plugin that exposes a few of the WP-Super-Cache functions to the outside world as an RPC interface. This enables you to control a few WP-Super-Cache functions like disabling cache, enabling cache, clearing cache and checking cache status from another program. This program can be a desktop Adobe AIR application, a JavaScript plugin or another PHP application. The code for the complete plugin is shown below.

To make this all work you will first have to activate the XML-RPC interface for your WordPress blog. You can activate the same from the admin > settings > writing > Remote Publishing section. The XML_RPC interface is controlled by your ‘xmlrpc.php’ file on your server.

<?php
 
/*
Plugin Name: Wp SuperCache API
Plugin URI: http://www.codediesel.com
Description: Expose a few Wp-SuperCache function for RPC
Version: 0.8
Author: Sameer Borate
Author URI: http://www.codediesel.com
*/
 
/* Add your custom RPC method to the Wordpress RPC methods list */
add_filter('xmlrpc_methods', 'my_xmlrpc_methods');
 
function my_xmlrpc_methods($methods) {
	$methods['wpSuperCacheRPC'] = 'my_cacheStatus';
	return $methods;
}
 
function my_cacheStatus($args)
{
    // Parse the arguments, take note that they're in the correct order
    $username	= $args[0];
    $password	= $args[1];
    $action	= $args[2];
 
    global $wp_xmlrpc_server;
 
    // Let's run a check to see if credentials are okay
    if (!$wp_xmlrpc_server->login($username, $password)) {
        return -1;
    }
 
    if(function_exists('wp_cache_is_enabled')) {
        global $cache_path;
 
        switch($action) {
            case "cache_status"  : return wp_cache_is_enabled(); 
                                   break;
            case "cache_enable"  : wp_cache_enable(); 
                                   break;
            case "cache_disable" : wp_cache_disable(); 
                                   break;
            case "cache_clear"   : prune_super_cache($cache_path, true); 
        }
    }
}
 
?>

Note that the functions I’ve used in the plugin: wp_cache_is_enabled(), wp_cache_enable(), wp_cache_disable(), prune_super_cache() are defined in the WP-Super-Cache plugin to accomplish their respective tasks.

RPC interface client

A sample PHP RPC client to work with the above code is shown below. Here we request the ‘cache_status’ function from the remote server.

<?php
 
/* XML_RPC Client to access the above plugin functions */
 
function wpSuperCacheRequest($rpc_url,$username,$password, $action)
{
    $params = array($username,$password,$action);
    $request = xmlrpc_encode_request('wpSuperCacheRPC',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpc_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
 
$rpc_server = "http://your-wordpress-site.com/xmlrpc.php";
 
/* Return '0' if cache is disabled or  '1' if enabled */
$action = 'cache_status';
 
/**
  * Action can be any of the following :
  *
  * cache_status
  * cache_enable
  * cache_disable
  * cache_clear
  */
 
/* 'USER', 'PASSWORD' is your WordPress admin credentials */
$data = wpSuperCacheRequest($rpc_server,'USER','PASSWORD', $action);
echo $data ;
 
?>

If you have never used a XML_RPC code on your machine you could get the following error after you run the above client program.

Fatal error: Call to undefined function xmlrpc_encode_request()

To rectify just enable the following line in your php.ini (on Windows).

extension=php_xmlrpc.dll

Testing the interface

Now when you run the above client program, it will connect with the remote WordPress site with the help of the plugin and control the wp-super-cache functionality.

When you use the ‘cache_clear’ function in the above client program and you want to check if the cache is actually cleared on the web server, make sure that you click on the Regenerate cache stats link in the wp-super-cache admin section to refresh the status. Not doing so will probably show you the old data and you may wonder why the code is not working.