WordPress.org

Forums

Passing Arguments to add_action? (8 posts)

  1. b-rad
    Member
    Posted 4 years ago #

    How do I pass arguments to add_action? I wanted to create an array of hooks, and then setup add_action calls to each array item like:

    $hook_array = array('publish_page','trash_page');
    foreach($hook_array as $hook)
    {
    	add_action($hook, 'my_function');
    }

    But this doesn't allow me to do anything within my_function based on the hook that was called. I know I could create individual functions and then tailor each one to the hook it works with, but then I'd be needlessly duplicating code and the more hooks I add to my hook array the more the code would grow.

  2. guar
    Member
    Posted 4 years ago #

    The arguments are defined by the do_action function as in the example here: http://codex.wordpress.org/Function_Reference/do_action

    So somewhere you'd need:

    do_action('publish_page',$arg1,$arg2);

    And your function should look something like:

    function my_function($arg1,$arg2)
    {
       // do something with the args
    }

    And your add_action should be something like:

    add_action($hook,'my_function',10,2);

    Where the "2" is the number of arguments.

  3. b-rad
    Member
    Posted 4 years ago #

    Hmmm, either I'm doing it wrong or it doesn't work. I tried

    $hook_array = array('publish_page','trash_page');
    foreach($hook_array as $hook)
    {
    	do_action($hook, $hook);
    	add_action($hook, 'my_function', 10, 1);
    }

    I would think that doing it this way would allow me to pass the hook name to my_function but it doesn't. Oddly enough, what seems to actually get passed to my_function is the page id.

  4. guar
    Member
    Posted 4 years ago #

    That wouldn't work because you're adding the action after the do_action call. You should have something like:

    $hook_array = array('publish_page','trash_page');
    foreach($hook_array as $hook)
    {
        add_action($hook, 'my_function', 10, 1);
        do_action($hook, $hook);
    }
    
    function my_function($arg)
    {
        echo $arg;
    }

    It doesn't do very much though, other than printing the name of the hook.

    I'm not sure that this will do anything particular useful anyway, because presumably you want the actions to do different things and will have different numbers of variables, which will vary according to the context.

  5. b-rad
    Member
    Posted 4 years ago #

    That sort of worked. The issue now is that while the passed parameter is correct, the call to the hook is being repeated many times. 13 times would be the count I received however I'm not sure if they're all from the same page, or coming from the page loading itself after a new page is added and then me going to check the plugin page. Seems that navigating the admin interface causes the add_action to trigger, or is that what do_action is doing, even when I'm not publishing or trashing a page? Basically I just want to know when any administrative function has occurred and log it.

  6. guar
    Member
    Posted 4 years ago #

    If you've just put in that code into your functions.php file then yes, it will run with every page load.

    The do_action function normally resides inside other functions where and when you want something particular to happen.

    Perhaps you want to link into WordPress' default hooks (you'll find a nice list here: http://adambrown.info/p/wp_hooks/hook/actions) such as post_updated. All you need do then is use add_action('post_updated','my_function') to do whatever funky stuff you want to do.

  7. b-rad
    Member
    Posted 4 years ago #

    Thanks for your help. The funky stuff I want to do is just know when some activities on the admin side have been performed (post published, page trashed, etc.) There are a lot of hooks so I wanted to avoid creating a function for each hook since I don't need to do anything with the hook action per se, but just rather know when the hook action was triggered. I suppose I could go the route of writing functions for each admin hook action but that just seems like a long way to go about doing this. By the way, none of this is in my functions.php -- I'm going the plugin route.

  8. guar
    Member
    Posted 4 years ago #

    That will certainly create a lot of data, perhaps the more useful of which is to know the order the actions are executed. Good luck with that.

Topic Closed

This topic has been closed to new replies.

About this Topic