WP Plugin: using add_filter inside a class

I am using WP v3.3.1 and I am trying to create a plugin. I got him half-working. It was triggered, and add_action works, but for some reason my filters do not start. When I searched googled around, I saw that I should have done it like this, but it doesnโ€™t work. I also tried to include it outside the class, which didn't work either. The error log is written from the constructor, but not from xmlAddMethod. I tested the xmlrpc call in a single file, and it worked, but with problems creating classes.

//DOESN'T WORK HERE
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );

class TargetDomain extends Domain 
{
    public function __construct() 
    {        
        error_log('TARGET: __construct');
        //DOESN'T WORK HERE EITHER
        add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
        parent::__construct();
    }

    function xmlAddMethod( $methods ) 
    {
        error_log('TARGET: xml_add_method');
        $methods['myBlog.publishPost'] = 'publishMyPost';
        return $methods;
    }
+3
source share
2 answers

Change this:

add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );

To:

add_filter( 'xmlrpc_methods', array( 'TargetDomain', 'xmlAddMethod') );
+7
source

You can also use the php magic __ CLASS __ constant .

add_filter( 'xmlrpc_methods', array( __CLASS__, 'xmlAddMethod') );
+1

All Articles