admin管理员组

文章数量:1194132

I'm pretty stumped on this one. I'm using add_action inside my plugin class to do certain things- add scripts & styles to the head, wp_ajax, etc. Here's the actions, in the __construct:

function __construct(){
    add_action('admin_menu', array($this, 'sph_admin_menu'));
    add_action('sph_header', array($this, 'sph_callback'));
    add_action('sph_header_items', array($this, 'sph_default_menu'), 1);
    add_action('sph_header_items', array($this, 'sph_searchform'), 2);
    add_action('sph_header_items', array($this, 'sph_social'), 3);

    //Below here they don't work. I have to call these outside of the class (but I need class variables within the functions)
    add_action('wp_print_styles', array(&$this, 'sph_stylesheets'));
    add_action('wp_print_scripts', array(&$this, 'sph_scripts'));
    add_action( 'wp_ajax_nopriv_add_to_list', array(&$this, 'le_add_to_list'));
    add_action( 'wp_ajax_add_to_list', array(&$this, 'le_add_to_list'));
    add_action('init', array(&$this, 'register_menu'));
}

Anybody ever come across something like this? I'd really like to know how to use said hooks from within a class- it's so messy having actions outside the class!

I'm pretty stumped on this one. I'm using add_action inside my plugin class to do certain things- add scripts & styles to the head, wp_ajax, etc. Here's the actions, in the __construct:

function __construct(){
    add_action('admin_menu', array($this, 'sph_admin_menu'));
    add_action('sph_header', array($this, 'sph_callback'));
    add_action('sph_header_items', array($this, 'sph_default_menu'), 1);
    add_action('sph_header_items', array($this, 'sph_searchform'), 2);
    add_action('sph_header_items', array($this, 'sph_social'), 3);

    //Below here they don't work. I have to call these outside of the class (but I need class variables within the functions)
    add_action('wp_print_styles', array(&$this, 'sph_stylesheets'));
    add_action('wp_print_scripts', array(&$this, 'sph_scripts'));
    add_action( 'wp_ajax_nopriv_add_to_list', array(&$this, 'le_add_to_list'));
    add_action( 'wp_ajax_add_to_list', array(&$this, 'le_add_to_list'));
    add_action('init', array(&$this, 'register_menu'));
}

Anybody ever come across something like this? I'd really like to know how to use said hooks from within a class- it's so messy having actions outside the class!

Share Improve this question edited May 1, 2012 at 11:26 fuxia 107k38 gold badges255 silver badges459 bronze badges asked May 1, 2012 at 7:21 HarleyHarley 1611 silver badge3 bronze badges 7
  • 3 were do you create an instance of this class? ex: $my_plugin = new MYClass(); since I've used these same hooks from within a class with no problems. – Bainternet Commented May 1, 2012 at 7:35
  • 1 Additionally, make sure that functions serving as the hooks have public visibility. – Joseph Leedy Commented May 1, 2012 at 12:43
  • Bainternet yeah I am. @Joseph that could be it. can construct be public? Cheers – Harley Commented May 2, 2012 at 4:22
  • @Harley - bainternet was asking where do you create an instance. – Stephen Harris Commented May 2, 2012 at 7:43
  • @Harley if you do not include a visibility modifier, it is automatically set to public. I was referring to the actual function the performs whatever action is being hooked. – Joseph Leedy Commented May 2, 2012 at 12:23
 |  Show 2 more comments

2 Answers 2

Reset to default 9

Sometimes certain hooks need to be fired at certain times. Example, some hooks need to be fired upon init.

Add this to your __construct()

add_action('init', array(&$this, 'init'));

Then add this function, which will contain all hooks that need to be fired upon init.

public function init(){
    add_action('hook_name', array(&$this, 'your_method_name'));
    add_action('hook_name', array(&$this, 'your_method_name'));
    add_action('hook_name', array(&$this, 'your_method_name'));
    add_action('hook_name', array(&$this, 'your_method_name'));
}

Another Example:

add_action( 'init', function () {

    add_action( 'hook_name', 'function_name', 10, 3 );
    add_action( 'hook_name', __NAMESPACE__ . '\namespaced_function_name', 10 );
    add_action( 'hook_name', '\specific\namespace\function_name', 5 );

}, 1 );

You will want to read about the hooks and when they are fired. So you know when and where to trigger your actions. Plugin API/Action Reference

This is a pretty old question, but in case anyone is looking for an answer, I had a similar issue. I had a class

class Plugin{
  function __construct(){
    add_action('init', array(&$this, 'init'));
  }

  function init(){
    // code...
  }
}

Plugin::init() was never getting called. I then realized my mistake. To instantiate the class I was doing this:

if(class_exists('Plugin')){
    add_action("init", "plugin_init");
    function socialsports_init() {
      global $plugin;
      $plugin = new Plugin;
    }
}

To fix it, I just changed the instantiation code to:

if(class_exists('Plugin')){
    add_action("init", "plugin_init");
    function socialsports_init() {
      global $plugin;
      $plugin = new Plugin;
      $plugin->init();
    }
}

The other option would be to use a different hook in the constructor:

function __construct(){
  add_action('wp_loaded', array(&$this, 'init'));
}

Or an earlier hook in the instantiation:

add_action("plugins_loaded", "plugin_init");

本文标签: actionsWhy do some hooks not work inside class context