facebook twitter flickr rss

Changing Your WordPress Theme Using XML-RPC, Part III - April 2nd, 2013

This post is a follow-up to the previous postings discussing how to allow WordPress to change themes using XML-RPC. Part I and Part II talk about the creation of a WordPress plugin, so that we can enable these calls. This post is intended to focus on the creation of a client, to communicate with WordPress. In this case I have chosen the Android mobile platform, mainly because I own an Android device and it is fairly straightforward and less expensive to create and test. Forget paying a hundred dollars a year for a developer’s license.

I’ve posted a walkthrough of the app on Youtube:

This app uses the android-xmlrpc Android Java library to easily create XML-RPC calls. Demonstrations on how to use this library can be found in this Codeforest article. This article also helped get me started with building a Android XML-RPC client application, although it does not focus specifically on WordPress.

The WordPress plugin that I created in the last two articles can be found on my Github account, as well as this client for your perusing. If you have any questions, shoot me a comment and I’ll try to respond in a timely fashion.

All code was written using the Eclipse IDE, which offered a good set of training wheels for Java beginner like me.

This app is not quite ready for production. If you choose to compile and use this app on your own, it is preferred that you do this with a WordPress site with an SSL certificate. I bear no responsibility for your site getting hacked because of a man-in-the-middle attack. Be safe and be smart.


Posted in Thoughts | No Comments »

Changing Your WordPress Theme Using XML-RPC, Part II - January 27th, 2013

Last week I talked about how to enable getting theme names within WordPress using XML-RPC. So now our imaginary mobile app can retrieve a list of theme names from WordPress, but we want to switch the theme once a name is selected. We’ll have to create another function and assign it to method to be called.

WordPress has another function called switch_theme, so this time around we will be making this available. Here is our plugin with the additional code:

<?php
/*
Plugin Name: XML-RPC Theme Changer
Plugin URI: https://github.com/dailyinvention/xml-rpc-theme-changer
Description: Allows the ability to get and switch themes using XML-RPC.
Author: Stefan Holodnick
Author URI: http://blog.dailyinvention.com
Version: 1.0
*/
  
function theme_get_themes($params) {
  global $wp_xmlrpc_server;
  $wp_xmlrpc_server->escape( $params );
  
  $username = $params[0];
  $password = $params[1];
  
  if ( ! $user = $wp_xmlrpc_server->login( $username, $password ) ) {
    return $wp_xmlrpc_server->error;
  }
  else {
    return wp_get_themes(false, true);
  }
  
}
  
function theme_switch_themes($params) {
  global $wp_xmlrpc_server;
  $wp_xmlrpc_server->escape( $params );
  
  $username = $params[0];
  $password = $params[1];
  $theme = $params[2];
  
  if ( ! $user = $wp_xmlrpc_server->login( $username, $password ) ) {
    return $wp_xmlrpc_server->error;
  }
  else {
    switch_theme( $theme );
  }
}

  
function theme_methods($methods) {
  $methods['themes.getThemes'] = 'theme_get_themes';
  $methods['themes.switchThemes'] = 'theme_switch_themes';
  return $methods;
}
  
add_filter('xmlrpc_methods', 'theme_methods');
  
?>

As you can see the theme_switch_themes function that I added is very similar to the theme_get_themes function. It grabs a username and password sent by a client, sanitizes those values, and verifies the user with WordPress. If the username and password do not match up, then it returns an error, otherwise it runs the switch_themes function. You may also see an extra parameter called theme. This is the theme name that a user will send to WordPress through their mobile app. The switch_theme function then switches the theme to the name provided.

You’ll notice that I then include my function within the list of methods, giving it the name “themes.switchThemes”. Again this is the method that your client application will call to trigger a theme switch.

Now we have a plugin that allows a remote application to get and switch themes within WordPress. I’ve included the complete plugin at my Github account, if you would like to try it out. Maybe you may have a different idea for using WordPress’s XML-RPC commands. You can take this example and are free to do what you want with it. Some of the authentication code I used was directly from WordPress’s XML-RPC documentation.

Also, a word on security. Having an SSL certificate for your WordPress installation is preferred when doing XML-RPC calls. Although it is about as risky as logging in to a non-SSL encrypted WordPress site, you are still subject to man-in-the-middle attacks.

The next part of this tutorial, I will be talking about creating a mobile app that interacts with WordPress and uses the methods we have created to make the magic happen.


Posted in Thoughts | No Comments »

Changing Your WordPress Theme Using XML-RPC, Part I - January 16th, 2013

I’ve had a project in mind for a little while that involves changing WordPress Themes using a mobile device. When Christmas comes, wouldn’t it be nice to switch your site over to a Christmassy theme with the push of a button? Or maybe use it to give your client the ability to quickly change themes when they want to. The nice thing about WordPress is that something like this is possible! And it can all be accomplished using XML-RPC calls.

XML-RPC (Remote Procedure Call) is a protocol used to communicate with web services, which is sent and received in XML form. You may have come into contact with desktop or mobile applications like Windows Live Writer that allow you to update WordPress. These applications use XML-RPC to add, edit and delete blog posts. In fact, WordPress has a number of methods you can access right out of the box. When I looked at the API, I was disappointed to not see any of the theme get and set functions available to execute remote procedures on. Looking a little deeper, I discovered that you can make the WordPress’s theme functions available.

I’m extending WordPress’s XML-RPC functions to include themes using a plugin, which if you’re not used to building plugins could sound daunting, but it really isn’t. I’m starting this out by extending WordPress’s wp_get_themes function to a method, so I can retrieve the list of themes in WordPress using my phone.

You always start each plugin with a header so it can be identified and displayed within WordPress:

<?php
/*
Plugin Name: XML-RPC Theme Changer
Plugin URI: https://github.com/dailyinvention/xml-rpc-theme-changer
Description: Allows the ability to get and switch themes using XML-RPC.
Author: Stefan Holodnick
Author URI: http://blog.dailyinvention.com
Version: 1.0
*/

?>

Now I’m going to add a function that runs the wp_get_themes function:

<?php
/*
Plugin Name: XML-RPC Theme Changer
Plugin URI: https://github.com/dailyinvention/xml-rpc-theme-changer
Description: Allows the ability to get and switch themes using XML-RPC.
Author: Stefan Holodnick
Author URI: http://blog.dailyinvention.com
Version: 1.0
*/
  
function theme_get_themes($params) {
  global $wp_xmlrpc_server;
  $wp_xmlrpc_server->escape( $params );
  
  $username = $params[0];
  $password = $params[1];
  
  if ( ! $user = $wp_xmlrpc_server->login( $username, $password ) ) {
    return $wp_xmlrpc_server->error;
  }
  else {
    return wp_get_themes(false, true);
  }
}

?>

WordPress has a built-in class for handling XML-RPC security called $wp_xmlrpc_server. It is being made accessible in the function by assigning it as global. Below that the $parameters value being sent to WordPress is sanitized using the escape method. We are sending an array of values to WordPress that include a username and password. If WordPress does not find the username and password to be true, it returns an error. Otherwise it executes wp_get_themes. You’ll see that I have defined “false” and “true” as arguments in the wp_get_themes function. With these values it will return an array of theme names from WordPress that don’t have errors and are allowed to be used.

Now we assign that function to a method that we can call:

<?php
/*
Plugin Name: XML-RPC Theme Changer
Plugin URI: https://github.com/dailyinvention/xml-rpc-theme-changer
Description: Allows the ability to get and switch themes using XML-RPC.
Author: Stefan Holodnick
Author URI: http://blog.dailyinvention.com
Version: 1.0
*/
  
function theme_get_themes($params) {
  global $wp_xmlrpc_server;
  $wp_xmlrpc_server->escape( $params );
  
  $username = $params[0];
  $password = $params[1];
  
  if ( ! $user = $wp_xmlrpc_server->login( $username, $password ) ) {
    return $wp_xmlrpc_server->error;
  }
  else {
    return wp_get_themes(false, true);
  }
}
  
function theme_methods($methods) {
  $methods['themes.getThemes'] = 'theme_get_themes';
  return $methods;
}

?>

You’ll see “themes.getThemes”. This is the method name that you reference when you do a remote call. And now the finishing touches:

<?php
/*
Plugin Name: XML-RPC Theme Changer
Plugin URI: https://github.com/dailyinvention/xml-rpc-theme-changer
Description: Allows the ability to get and switch themes using XML-RPC.
Author: Stefan Holodnick
Author URI: http://blog.dailyinvention.com
Version: 1.0
*/
  
function theme_get_themes($params) {
  global $wp_xmlrpc_server;
  $wp_xmlrpc_server->escape( $params );
  
  $username = $params[0];
  $password = $params[1];
  
  if ( ! $user = $wp_xmlrpc_server->login( $username, $password ) ) {
    return $wp_xmlrpc_server->error;
  }
  else {
    return wp_get_themes(false, true);
  }
}
  
function theme_methods($methods) {
  $methods['themes.getThemes'] = 'theme_get_themes';
  return $methods;
}
  
add_filter('xmlrpc_methods', 'theme_methods');
?>

Now that we have a method, we need to include it within WordPress’s XML-RPC methods. This “add_filter” function does just that.

Not too bad, was it? Now that WordPress is set up to retrieve the theme names, we probably want our mobile app or web client to be able to switch the current theme when it is selected. The next part of this tutorial I’ll cover the process of enabling WordPress to switch themes over XML-RPC.


Posted in Thoughts | No Comments »

Handling Large KML Files Without Google Maps - November 14th, 2012

In the four weeks or so, I’ve become quite familiar with the Google Maps API, pitfalls included. I’ve been contracted to work on an interactive map for the RAIN Group, a not-for-profit out of Pennsylvania that is focused on water quality. Especially in areas where hydraulic fracturing (fracking) for natural gas is taking place.

Google’s API, to say the least, is easy to customize with some javascript experience. The requirements of the project required overlays gathered from USGS, EPA and other entities. It was fairly straight forward to incorporate the KML layers that we procured, but some of these files ran rather large in size. After the heartbreak of having to admit that Google was telling the truth about the size limitations on KML files, it was back to the drawing board.

Luckily, after a lot of hunting, I found the GeoXML library. Rather than using Google’s servers to render these layers, GeoXML provided a client-side alternative. This means that the size limitations that we faced were no longer an obstacle in displaying our layers.

Everything came together and the world was at peace again. If you are considering building your own map elements from complex datasets, I would not overlook this library. It will save your life… or at least a little headache.


Posted in Thoughts | No Comments »

Utilizing CAP (Common Alert Protocol) - June 20th, 2012

Rave Alert has been a major project that I have have been assigned to implement. This system provides an easy way of contacting students and staff in case of emergency via email, text message, voice call, Facebook and Twitter. We also use it for snow days and class cancellations. Not too long ago the helpful staff at Rave introduced me to the CAP XML standard. CAP is a standard not just specific to Rave, but many emergency alert systems. It is also used by various government agencies for sharing emergency information over multiple systems.

PHP CAP Alert Function

Once I heard of CAP, I knew we could use this XML standard in various ways to extend the reach of our own emergency alerts. One being the school web page. Here is a small function I put together using PHP that will display an emergency message on a web page:

Just include this code on your PHP page and then call it with the CAP URL location you want to pull from:

cap_parse("http://example.com/cap.xml");

CAP Desktop Alerting Software

Taking this a step further, I decided to get a little more familiar with .NET. It was brought to my attention that we were missing a desktop alert component. There are server-based systems out there that require some setup and a good deal of money. After a little thought, I concluded we could meet these same goals with a simple desktop client that used the CAP XML feed to trigger an emergency message. And in a couple days I had something built.

I thought this project warranted creating a Github repository. It’s still a little rough, but by bringing it to git, I’m hoping to get some more interest and input in this project. I’ve made the source code available, so maybe someone will find this code useful to bolster their own emergency alerting using CAP. Please visit my CAP Alerting Desktop repository if you are interested. Any contributions to the code are welcome.

Mentioned Resources:

PHP CAP Alert Function
CAP Desktop Alerting Software


Posted in Thoughts | 2 Comments »