Accessing WordPress plugins remotely

With a plethora of wordpress plugins available for every conceivable purpose, there is a huge amount of data that is stored on a typical wordpress installation. We could easily use that data across domains or display them using widgets on the desktop. Take for example the WP-UserOnline plugin that displays how many users are currently online on your blog. We could easily write a proxy to grab that information from the plugin and use it in a Yahoo Widget or in a AIR application on the desktop, so you can see the number of users online without having to visit your site. Or you could use the information on some other site.

Example proxy
A sample proxy code to access the WP-UserOnline plugin data is shown below. The code basically uses the WP-UserOnline plugins ‘get_users_browsing_site()‘ function to read the data and echo it as a JSON string. Now all you have to do is request the below file (useronline_proxy.php) from a remote server and you get a JSON string containing the useronline information.

<?php
 
/* 
  useronline_proxy.php
 
  On my server this file is located in the
  www.codediesel.com/remote/ directory, hence the
  location of the $wp_root variable. You will need
  to change it depending on where you save this file.
*/
$wp_root = '..';
 
if (file_exists($wp_root.'/wp-load.php')) {
	require_once($wp_root.'/wp-load.php');
} else {
	require_once($wp_root.'/wp-config.php');
}
 
/*  The following file contains all the functions 
    we require to get to the wp-useronline data. 
*/ 
require_once($wp_root.
             '/wp-content/plugins/wp-useronline/wp-useronline.php');
 
/* Get the number of users browsing the site right now */
$users = get_users_browsing_site(false);
 
$temp = array("member"  =>  $users[0],
              "guest"   =>  $users[1],
              "bot"     =>  $users[2],
              "total"   =>  array_sum($users));
 
 
/* Encodes the array as a JSON string and print it.
  (Only available since PHP 5.2)
*/
echo json_encode($temp);
 
?>

How the whole thing works is shown below:

wordpress remote

Accessing the proxy remotely
Assuming allow-url-fopen is true, you can now access the useronline_proxy.php file from another server as below. Note that you will have to use cURL if remote url fopen is not allowed on your php installation.

<?php
 
$fp = fopen("http://www.domain.com/remote/useronline_proxy.php", "r");
 
$json_string = '';
 
while (!feof($fp ))
{
  $json_string .= fread($fp, 8192);
}
 
$json_data = json_decode($json_string);
 
fclose($fp);
 
?>

Using Javascript to access the proxy
You can also use javascript to access the remote proxy. But since cross domain access is restricted by the browser, we will need to resort to jQuery’s getJSON function to do the same. The javascript code is shown below.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title></title>
<script src="jquery-1.3.2.js"></script>
</head>
<body>
<script>
$(document).ready(function()
{
 
$proxy = "http://www.domain.com/remote/useronline_proxy.php?callback=?";
    $.getJSON($proxy, function(data)
    {
        $info = 'Members: ' + data.member + '\n' +
                'Guests: ' + data.guest + '\n' +
                'Bots: ' + data.bot + '\n' +
                'Total: ' + data.total;
 
        alert($info);
    });
});
</script>
</body>
</html>

But to make the above javascript code work we will have to make a little change to the useronline_proxy.php file.

 
/* Replace the 'echo json_encode($temp);' line with the following */
 
echo $_GET['callback'] . '(' . json_encode($temp) . ');';

Security
To keep the proxy code simple, I’ve not added any authentication to the same. But you could pass a username/password as a GET parameter and check it in useronline_proxy.php.