Accessing Wordpress plugins remotely


Posted in: php | Save to del.icio.us | Twit This! 21 Jun 2009

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.




Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts



3 Responses

1

Accessing Wordpress plugin data across domains : CodeDiesel | bllogger

June 22nd, 2009 at 10:48 am

[...] the original:  Accessing Wordpress plugin data across domains : CodeDiesel Share and [...]

2

CSS Brigit | Accessing Wordpress plugin data across domains

June 22nd, 2009 at 6:27 pm

Accessing Wordpress plugin data across domains…

How to access wordpress plugins data from other domains and from desktop widgets.

3

Bookmarks for June 22nd through June 23rd | Maximi maxima maxi max ist da

June 23rd, 2009 at 2:33 pm

[...] Accessing Wordpress plugin data across domains : CodeDiesel – [...]

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: My apologies! I'm not conversant with SharePoint. [...]
  • avanthi: Is it possible to automate share point people picker control through selenium. When i record throug [...]
  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]

  • Users Online

    • 6 Users Online
    • 5 Guests, 1 Bot