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.

              "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.

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.

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) . ');';

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.