0

according to this Callback example if I'm referencing a method inside a class, I should use add_filter by specifying an array with 2 elements, first the object then the method. Here is my code:

add_action ('login_head', array('Admin', 'plugin_setup'));

class Admin 
{
   public function plugin_setup()
   {
      add_filter('login_headerurl', array($this, 'the_logo_url'));   
   }

   private function the_logo_url()
   {
      return get_bloginfo('url');
   }

unfortunately my code does not seem to work unless I move the function the_logo_url() outside the class. What's the best way of approaching this problem?

  • 1
    Change private to public and make plugin_setup() static. – birgire May 5 '15 at 19:09
  • @birgire: I get yet the following msg error PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object – Tetravalente May 5 '15 at 19:14
1

Here's a slightly modified version of your code snippet:

add_action( 'login_head', [ 'WPSE_Admin', 'plugin_setup' ] );

class WPSE_Admin 
{
   public static function plugin_setup()
   {
      add_filter( 'login_headerurl', [ 'WPSE_Admin', 'the_logo_url' ] );   
   }

   public function the_logo_url()
   {
      return get_bloginfo('url');
   }
}

The filter callbacks must be public, not private. The reason for this is that apply_filters()/apply_filters_ref_array() are running call_user_func_array() on the stored filter callbacks, in the global $wp_filter array.

Also notice that you're not instantiating your WPSE_Admin class, so you can't use $this. You might want to use namespace but I just prefix the class here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.