WordPress.org

Support

Support » Plugins and Hacks » [Resolved] [help] sink plugin hooks for $this references in WordPress 2.0 ?

[Resolved] [help] sink plugin hooks for $this references in WordPress 2.0 ?

  • foo bar
    Member

    @nielshackius

    Hej people, what am I doing wrong on this one:
    In the Codex it says:
    You can also sink plugin hooks to object instances. Here is an example of how to do this using the class above:

    $mailer = new emailer();
    add_action(‘publish_post’, array(&$mailer, ‘send’));

    Whenever a post’s status is set to “Publish”, WordPress will execute the send() method of the $mailer object. Used this way, the class can make use of $this-> references, whereas the static calling method above can only call the class methods statically using the scope resolution operator.

    I am doing this
    $lbnh_installer_reference = new loadbalancer();

    add_action('activate_samba/loadbalancer.php',array(&$lbnh_installer_reference,"installer"));

    And I am also getting a confirmation that the constructor of the loadbalancer class has been run, however it doesn’t run the installer function.
    I am doing this, because I am wanting to use $this references within the class.

    Help would be nice or maybe this isn’t possible in WordPress 2.0 anymore?

Viewing 6 replies - 1 through 6 (of 6 total)
  • add_action(‘activate_samba/loadbalancer.php’,array(&$lbnh_installer_reference,”installer”));

    first, you’d want it to look like:
    add_action(‘activate_samba’,array(&$lbnh_installer_reference,”installer”));

    second, I don’t know of an activate_samba action. Is that something that you are calling do_action for? if not, obviously you need to attach to an existing action for your object to be called.

    -d

    foo bar
    Member

    @nielshackius

    ‘add_action(‘activate_samba’,array(&$lbnh_installer_reference,”installer”));’
    appears to work too, nice. However this doesn’t solve my problem at all.
    the activate_{pluginname} action hook is described here:

    http://codex.wordpress.org/Creating_Tables_with_Plugins#Calling_the_function

    Thanks for the help.

    You might want to look at the WP function “register_activation_hook($file, $function)”. That does the work of making the action call, not sure if it is taking a full path (since your original test failed), or filename (thus ‘activate_loadbalancer.php’), or the file prefix (‘activate_loadbalancer’). You could test the variations pretty quickly…

    Otherwise, write to the wp-hackers mailing list, as you’re more likely to get a detailed answer there.

    -d

    foo bar
    Member

    @nielshackius

    okay..it got solved on the mailing list. Anyone interessted in the solution should check here:
    http://comox.textdrive.com/pipermail/wp-hackers/2006-January/004032.html

    I didn’t see a solution there…

    foo bar
    Member

    @nielshackius

    ..well okay its kind of a solution.
    The simple answer: The codex is wrong in this matter. You are actually suppossed to use register_activation_hook(__FILE__,$function) on this. Whereas $function can’t be the array(&$reference, $functionname) a reference to a class-variable. The solution to still sink $this in this hook is to call it in the constructor like this:
    register_activation_hook(__FILE__,array(&$this,$functionname))

    Its not like it is described and like this wasn’t enough. IN 2.0 there are also errors in the register_activation_hook-function or at least in the plugin_basename-function which it uses.
    So instead of register_activation_hook you will need to use
    add_action("activate_{pluginname}",$function)

    And in case {pluginname} isn’t a plugin which is straight in the pluginfolder this is an example of usage:
    FOLDEROFYOURPLUGIN/plugin.php be aware of the normal slash.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] [help] sink plugin hooks for $this references in WordPress 2.0 ?’ is closed to new replies.