WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [help] sink plugin hooks for $this references in WordPress 2.0 ? (7 posts)

  1. foo bar
    Member
    Posted 8 years ago #

    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?

  2. davidchait
    Member
    Posted 8 years ago #

    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

  3. foo bar
    Member
    Posted 8 years ago #

    '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.

  4. davidchait
    Member
    Posted 8 years ago #

    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

  5. foo bar
    Member
    Posted 8 years ago #

    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

  6. davidchait
    Member
    Posted 8 years ago #

    I didn't see a solution there...

  7. foo bar
    Member
    Posted 8 years ago #

    ..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.

Topic Closed

This topic has been closed to new replies.

About this Topic