Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to add custom methods to the Xml-Rpc file and have found the following:


// Custom plugins add_filter('xmlrpc_methods', 'custom_xmlrpc_methods');

function custom_xmlrpc_methods($methods) { $methods['myMethod'] = 'my_function'; return $methods; }

Questions:

  • Is it possible to have the callback function in another file and if yes then how do you do that in the code?
  • If I have lots of custom methods what is the best approach to handling this?

Thanks Michael

share|improve this question
add comment

3 Answers

If I have lots of custom methods what is the best approach to handling this?

Instead of filtering xmlrpc_methods, you could extend the wp_xmlrpc_server class and set your class as default with the filter wp_xmlrpc_server_class.

// Webeo_XMLRPC.php
include_once(ABSPATH . WPINC . '/class-IXR.php');
include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php');

class Webeo_XMLRPC extends wp_xmlrpc_server {
    public function __construct() {
        parent::__construct();

        $methods = array(
            'webeo.getPost' => 'this:webeo_getPost',
            'webeo.getPosts' => 'this:webeo_getPosts'
        );

        $this->methods = array_merge($this->methods, $methods);
    }

    public static function webeo_getName() {
        return __CLASS__;
    }

    public function sayHello($args) {
        return 'Hello Commander!';
    }

    public function webeo_getPost($args) {
        // do the magic
    }

    public function webeo_getPosts($args) {
        // do the magic
    }
}

add_filter('wp_xmlrpc_server_class', array('Webeo_XMLRPC', 'webeo_getName'));
share|improve this answer
    
This is the best way to do it. Also this makes it very useful to override base methods like getPosts –  jontro Oct 31 '12 at 16:00
    
I'm getting a Fatal error: Class 'wp_xmlrpc_server' with this when trying it in my plugin. Did I miss a step with including or loading wp_xmlrpc_server when overriding? –  Nic May 6 '13 at 17:39
1  
@Nic You're probably calling your XMLRPC class too early. You could attach it to the action hook wp or simply add include_once(ABSPATH . WPINC . '/class-IXR.php'); and include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'); above your XMLRPC class. –  rofflox May 6 '13 at 19:22
    
am I right in saying that if another plugin were to register their class as the default xmlrpc-server then your code would get knocked of? Therefore this may not be the best solution? –  Coombesy May 28 '13 at 12:39
add comment

This doesn't realyl have much to do with WordPress, just generic PHP and your personal preferences in coding.

Is it possible to have the callback function in another file and if yes then how do you do that in the code?

As with any PHP code you can split it between files and load them with include.

If I have lots of custom methods what is the best approach to handling this?

As for me (if you are not using classes) single file with all functions organized in some way (for example by purpose ot alphabet) will do.

share|improve this answer
add comment

If you are looking to use other existing WordPress functions that aren't included in their XML-RPC, try using the plugin Extend XML-RPC API.

This plugin was made to make WordPress easier to integrate with external platforms and external code. While the existing WordPress XML-RPC API provides a lot of functionality, it does not provide everything. This plugin allows nearly every standard WordPress function to be called via API.

Otherwise, download that plugin and use the code as an example...it should be plenty for you to understand how to add your own plugins to the XML-RPC.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.