WordPress sidebars fetching on other cms with wordpress xml rpc

Recently I worked on a site which is using wordpress as it’s cms and creloaded as it’s web shop. So the wordpress was top of the shopping cart and then the shopping cart as webshop. So basically I had to develop the site using wordpress and then had to install that creloaded inside woordpress installation.

For main site wordpress has it’s header (with navigation), footer (with navigation and other contents), sidebar with widgets etc. So when I am visiting the web shop with creloaded I was required to show the same layouts and design. For that I had to use wp xmlrpc to fetch those common features into creloaded system.

XML-RPC functionality is turned on by default since WordPress 3.5. So anyone can use it who is using wp 3.5 or greater without activating xml-rpc via wordpress admin!

All of this can be done with below written 3 steps.
1. Writing XML Client Class
2. Using XML Client
3. Defining XML Server methods in wp functions.php

Note: Here all the things are not well documented, Because I am assuming you are enough expert on wp and did some google and finally here to see a code example of wp xml rpc implementations.

XML Client

//http://wp.tutsplus.com/tutorials/creative-coding/xml-rpc-in-wordpress/
class XMLRPClientWordPress
{
    var $XMLRPCURL = "";
    var $UserName  = "";
    var $PassWord = "";
    var $cacheLocation = "";
    var $enableCache = false;
    // Constructor
    public function __construct($xmlrpcurl, $username, $password, $enableCache, $cacheLocation)
    {
        $this->XMLRPCURL = $xmlrpcurl;
        $this->UserName  = $username;
        $this->PassWord = $password;
        $this->enableCache = $enableCache;
        $this->cacheLocation = $cacheLocation;
    }

    public function send_request($requestname, $params)
    {
        $request = xmlrpc_encode_request($requestname, $params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_URL, $this->XMLRPCURL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        $results = curl_exec($ch);
        curl_close($ch);
        return $results;
    }

    public function getOptions($id){
        $cacheFile = "Options.html";
        if($this->checkCacheAvailable($cacheFile)){
            $output = $this->readCache($cacheFile);
        } else {
            $content = array( $id );
            $params = array(0,$this->UserName,$this->PassWord,$content,true);
            $output = $this->send_request('wp.getOptions',$params);
            $this->writeCache($cacheFile, $output);
        }
        return $output;
    }

    public function getFooterMenu(){
        $params = array();

        $cacheFile = "FooterMenu.html";
        if($this->checkCacheAvailable($cacheFile)){
            $output = $this->readCache($cacheFile);
        } else {
            $output = $this->send_request('nsp.getFooterMenu', $params);
            $this->writeCache($cacheFile, $output);
        }

        return $output;
    }

    public function getHeaderMenu(){
        $params = array();

        $cacheFile = "HeaderMenu.html";
        if($this->checkCacheAvailable($cacheFile)){
            $output = $this->readCache($cacheFile);
        } else {
            $output = $this->send_request('nsp.getHeaderMenu', $params);
            $this->writeCache($cacheFile, $output);
        }

        return $output;
    }

    public function getStyleSheetUrl(){
        $params = array();

        $cacheFile = "StyleSheetUrl.html";
        if($this->checkCacheAvailable($cacheFile)){
            $output = $this->readCache($cacheFile);
        } else {
            $output = $this->send_request('nsp.getStyleSheetUrl', $params);
            $this->writeCache($cacheFile, $output);
        }

        return $output;
    }

    public function getTemplateUrl(){
        $params = array();

        $cacheFile = "TemplateUrl.html";
        if($this->checkCacheAvailable($cacheFile)){
            $output = $this->readCache($cacheFile);
        } else {
            $output = $this->send_request('nsp.getTemplateUrl', $params);
            $this->writeCache($cacheFile, $output);
        }

        return $output;
    }

    public function getFooterHtml(){
        $params = array();

        $cacheFile = "FooterHtml.html";
        if($this->checkCacheAvailable($cacheFile)){
            $output = $this->readCache($cacheFile);
        } else {
            $output = $this->send_request('nsp.getFooterHtml', $params);
            $this->writeCache($cacheFile, $output);
        }

        return $output;
    }

    public function getSidebarHtml(){
        $params = array();

        $cacheFile = "SidebarHtml.html";
        if($this->checkCacheAvailable($cacheFile)){
            $output = $this->readCache($cacheFile);
        } else {
            $output = $this->send_request('nsp.getSidebarHtml', $params);
            $this->writeCache($cacheFile, $output);
        }

        return $output;
    }



    public function checkCacheAvailable($name){
        if($this->enableCache){
            $cachefile = $this->cacheLocation . $name;
            $cachetime = 5 * 60; //5 min
            if (file_exists($cachefile) && (time() - $cachetime cacheLocation . $name;
        $output = file_get_contents($cachefile, FILE_USE_INCLUDE_PATH);
        return $output;
    }



    public function writeCache($name, $content){
        if($this->enableCache){
            $cachefile = $this->cacheLocation . $name;
            $fp = fopen($cachefile, 'w');
            fwrite($fp, $content);
            fclose($fp);
        }
    }

    /*
    *
    * @ param string $data Full html content which you want to parse
    * @ param string $s_tag Start tag of html content
    * @ param string $e_tag End tag of html content
    * @ return middle html content from given start tag and end tag of $data
    * */

    public function getValueByTagName( $data, $s_tag, $e_tag) {
            $s = strpos( $data,$s_tag) + strlen( $s_tag);
            $e = strlen( $data);
            $data= substr($data, $s, $e);
            $s = 0;
            $e = strpos( $data,$e_tag);
            $data= substr($data, $s, $e);
            $data= substr($data, $s, $e);
            return  $data;
    }
}

Including and using XML Client

if ( file_exists(TEMPLATE_FS_CUSTOM_INCLUDES . 'XMLRPClientWordPress.php') ) {
    $enableCache = false;
    $cacheLocation = DIR_FS_CATALOG . 'tmp/cache/';

    include(TEMPLATE_FS_CUSTOM_INCLUDES . 'XMLRPClientWordPress.php');
    $objXMLRPClientWordPress = new XMLRPClientWordPress('http://wpdomain.com/xmlrpc.php','wp_admin_username','wp_admin_password', $enableCache, $cacheLocation);
    $theme_option_name = 'nsp';
    $options = $objXMLRPClientWordPress->getOptions($theme_option_name);


    $wp_options_key_array = array('header_phone_numnber', 'facebook_link', 'twitter_link', 'google_plus_link', 'rss_link', 'copy_right_text', 'footer_text', 'siteurl');
    $wp_options = array();
    foreach($wp_options_key_array as $key=>$value){
        $wp_options[$value] =  $objXMLRPClientWordPress->getValueByTagName($options, $value.'', '');
    }
}

WP XML-RPC Server methods definitions in wp functions.php

/*Wordpress xml-rpc behavior for creloaded---*/
function nsp_getFooterMenu() {
    $menu_html = wp_nav_menu(array('container'=> 'nav','theme_location' => 'footer', 'echo'=>false));
    return $menu_html;
}
function nsp_getHeaderMenu() {
    $menu_html = wp_nav_menu(array('container'=> 'nav','menu_id' =>'nav', 'menu_class' =>'headerMenu','theme_location' => 'primary', 'echo'=>false));
    return $menu_html;
}
function nsp_getSidebarHtml() {
    $menu_html = get_sidebar_content_rcp();
    return $menu_html;
}
function nsp_getFooterHtml() {
    $menu_html = footer_contents();
    return $menu_html;
}
function nsp_getStyleSheetUrl() {
    $stylesheet_url = get_bloginfo('stylesheet_url');
    return $stylesheet_url;
}
function nsp_getTemplateUrl() {
    $template_url = get_bloginfo('template_url');
    return $template_url;
}

function nsp_new_xmlrpc_methods( $methods ) {
    $methods['nsp.getStyleSheetUrl'] = 'nsp_getStyleSheetUrl';
    $methods['nsp.getTemplateUrl'] = 'nsp_getTemplateUrl';
    $methods['nsp.getHeaderMenu'] = 'nsp_getHeaderMenu';
    $methods['nsp.getSidebarHtml'] = 'nsp_getSidebarHtml';
    $methods['nsp.getFooterMenu'] = 'nsp_getFooterMenu';
    $methods['nsp.getFooterHtml'] = 'nsp_getFooterHtml';
    return $methods;
}
add_filter( 'xmlrpc_methods', 'nsp_new_xmlrpc_methods');


add_filter('xmlrpc_blog_options', 'xmlrpc_blog_options_defination');
function xmlrpc_blog_options_defination($attr){
    $config = get_option( 'nsp' );
    $config['siteurl']=site_url();
    $xml_rpc_options['nsp'] = array('desc'=>'Current Theme Option Framework Options', 'readonly'=>1, 'value'=>$config);
    $attr = array_merge($attr, $xml_rpc_options);
    return $attr;
}
//end of xml-rpc behavior or extends code

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.