[wp-hackers] add_action arrays?

Chris Jean gaarai at gaarai.com
Wed Sep 23 12:11:42 UTC 2009

Passing array( &$this, 'method_name' ) as the callback argument is a way 
of passing a class method as the callback rather than just a flat function.

Using the $this variable would require a class instance, for example:

if ( ! class_exists( 'PluginTest' ) ) {
    class PluginTest {
        function PluginTest() {
            add_action( 'admin_menu', array( &$this, 'create_menu' ) );
        function create_menu() {
    new PluginTest();

I hope this helps answer your question.

Chris Jean

Thomas Belknap wrote:
> I've seen a few plugins and bits of code internal to WordPress use the
> following convention:
> add_action('pre_get_posts', array(&$this, 'postFilter'));
> But this usage is not documented anywhere? I attempted to use the same
> convention and got the following error:
> call_user_func_array() [function.call-user-func-array]: First argument
> is expected to be a valid callback, 'Array' was given in blah, blah,
> blah...
> I was looking at the code for add_action and add_filter, and they
> definitely seem to not take arrays into account as far as I can tell.
> Is this a deprecated way of doing things? Is there some other way to
> hook an object method into an action or filter?
> Thanks!
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list