Community Documentation

Using XML-RPC to combine Drupal and Wordpress on a site

Last updated May 17, 2011. Created by redmonk on July 18, 2006.
Edited by authentictech, Boris Mann, sepeck. Log in to edit this page.

This is really a set of notes for myself, so I can document what I'm trying to do and hopefully be able to reproduce it in the future.

The Idea

I have two sites right now - a Wordpress blog at /, and in /more, a Drupal site. I'm working on a new theme for both that includes content from each on the other, with some interesting ways to switch between them.

Starting Out

I have some ideas on grabbing Wordpress content from a Drupal page/template, so I'm concentrating on solving the other issues right now: starting Drupal and getting some content out of it. So far, I started a test.php file and have the folowing bits:

1. *Start Drupal:* Not too bad, Morbus helped me here.

    require_once (./includes/bootstrap.inc);
    drupal_bootstrap (DRUPAL_BOOTSTRAP_FULL);

2. *Grab some content to render:* I want to do as much config as possible in Drupal as to the content that appears on the Wordpress side, so I created new region in my theme, 'shared', and assigned a couple blocks to it. I then followed up the previous code with this:

    print theme('blocks', 'shared');

At this point I'm a bit stumped... I'm getting simply a blank page as the output. I suspect is has something to do with not having a template file to use, but I'm not at all certain, and I'd be open to any suggestions.

Cautious Success

Well, I figured out the problem I was experiencing in [Part I](/tech/franken-site-i) - I had misconfigured the blocks for the region I was attempting to use, so that's fixed. Using a chdir, my code now looked like this:

    $_DIR = 'more';
    chdir ($_DIR);
    require_once './includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    return theme('blocks', $region);

And it worked! At least, from a plain php page in my Wordpress directory it did. As soon as I built it into a quick-and-dirty Drupal plugin for Wordpress, it went all to heck.

    Fatal error: Cannot redeclare timer_start() (previously
    declared in /server/path/to/redmonk.net/wp-settings.php:57)
    in /server/path/to/redmonk.net/more/includes/bootstrap.inc
    on line 37

D'oh! Drupal and Wordpress were defining the same functions! To be expected, I suppose. I commented out the first few I found but kept running into more. Time to look for another solution.

Regroup and RPC

After my simple attempts at just including Drupal's bootstrap and using theme() to get what I wanted failed, I turned to a new pipe to get content from: Drupal's XML-RPC interface.

I have experience writing Drupal modules, so I did the required reading on hook_xmlrpc, and then got to work.

I added a custom module, redmonk.module. Then, I added a simple method to return the content I wanted (hard coded for now, later I'd make it a little more generic).

    function redmonk_shared () {
        return theme('blocks', 'shared');
    }

Then I implemented hook_xmlrpc:

    function redmonk_xmlrpc () {
        return array ("redmonk.shared"=>"redmonk_shared");
    }

Lastly, I built a quick Wordpress plugin, the expansively named drupal.php:

    include_once(ABSPATH . WPINC . '/class-IXR.php');

    function drupal_get_blocks ($region) {
        $ixrcli = new \
            IXR_Client('http://redmonk.net/more/xmlrpc.php', \
            false,80,15);
        $ret = "no response";
if ($ixrcli->query('redmonk.shared')) {
            $ret = $ixrcli->getResponse();
        }
       
return $ret;
    }

So now, I have an xml-rpc call that returns the chunk of HTML that I need for the Wordpress half of my franken-site. Next, I need to confirm that I can do something similar from Wordpress.

It's Alive!

After managing to fetch the content I wanted from Drupal into a Wordpress page via a custom XML-RPC call, I needed to figure out how to get recent posts out of Wordpress and onto a Drupal page.

Naturally RSS is a pretty decent way to accomplish this sort of thing, but I did not really want to mess with creating a custom feed just for this information. So I fell back to Wordpress's XML-RPC api. Wordpress supports the MetaWeblog api, which has an api which fitted my needs exactly:

metaWeblog.getRecentposts (n)

So, I added a couple methods to my redmonk.module for Drupal:

    function redmonk_monkinetic_posts_call ($num) {
        $rpc_result = xmlrpc ("http://redmonk.net/xmlrpc.php",
                              "metaWeblog.getRecentPosts",
                              "myblog","myusername","mypwd", $num);
        return $rpc_result;
    }

    function redmonk_monkinetic_posts ($num) {
        $posts = redmonk_monkinetic_posts_call ($num);
        $retstr = "<dl class='nodes'>\n";
        foreach ($posts as $post) {
            $retstr .= "<dt><a href='" . $post['link'] . "'>" .
                       $post['title'] . "</a></dt>\n";
            $retstr .= "<dd>" . $post['description'] . "</dd>\n";
        }
        $retstr .= "</dl>\n";
        return $retstr;
    }

The call goes into a Drupal template in the normal manner:

    print redmonk_monkinetic_posts (1);

Bingo!

So now all that remains is to clean up the plugin/module code on each side, and implement the new template. I *had* initially hoped for a more "integrated" way to work between the two, but the XML-RPC approach was remarkably simple, will be quite flexible, and will survive upgrades to both platforms.

Page status

Needs technical review

Log in to edit this page

About this page

Drupal version
Drupal 4.6.x, Drupal 4.7.x
Audience
Developers and coders

Installation guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here