Extending xml rpc – best practice

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

Read More

// 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

Related posts

Leave a Reply

3 comments

  1. 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'));
    
  2. 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.

  3. 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.