Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP Plugin: using add_filter inside of a class

Tags:

wordpress

I am using WP v3.3.1, and I am trying to make a plugin. I have gotten it semi-working. It initiated, and add_action works, but for some reason my filters don't get triggered. When I googled around, I saw I was supposed to do it like this, but it is not working. I also tried including it outside of the class, which didn't work either. The error log is written to from the constructor, but not the xmlAddMethod. I tested the xmlrpc call in a single file, and it worked, but having problems making 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;
    }
like image 638
Nathan Avatar asked Dec 04 '22 03:12

Nathan


2 Answers

Change this:

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

To:

add_filter( 'xmlrpc_methods', array( 'TargetDomain', 'xmlAddMethod') );
like image 67
maiorano84 Avatar answered Dec 20 '22 04:12

maiorano84


You can also use php's magic __CLASS__ constant.

add_filter( 'xmlrpc_methods', array( __CLASS__, 'xmlAddMethod') );
like image 27
ljsherlock Avatar answered Dec 20 '22 04:12

ljsherlock