Passing arguments with WordPress add_action

18th February 2011    

Most WordPress developers are familiar with using add_action, but as you may have discovered the information on passing arguments to your callback function is hard to find. After a couple of hours of Googling, throwing a hissy fit, and good old trial and error I found a working solution, so I thought I’d share.

The problem

I wanted to run a function on admin_init, which would need arguments passed as an array. Running the function is easy enough:

add_action( 'admin_init', 'my_function');

function my_function() {
//some code here
}

Add_action does accept a third parameter, but it’s not what I hoped it was. Instead it’s the number of arguments your function can accept. So how do you actually pass these arguments?

My solution

The answer appears to be using do_action, which is something I’d never personally worked with before, and using two functions. The first parameter of do_action is the hook name, followed by any arguments you want to pass.

add_action( 'admin_init', 'argument_function');

add_action( 'my_hook', 'my_function');

function my_function($argument) {
//some code here
}

function argument_function() {
$argument = 'something';
do_action('my hook', $argument);
}

Since I want to run my main function on admin_init, this is essentially a chain reaction.

The second add_action defines a custom hook, rather than one of the built-in WordPress ones, which I’m calling my_hook. On admin_init my argument_function is called, which passes the argument to my custom hook using do_action, which in turn triggers my_function but includes that argument.

If that last paragraph didn’t make your head explode, there’s one more thing to bear in mind. The third parameter of add_action is, as I mentioned, the number of accepted arguments. This defaults to 1, which is why it’s not needed in my example. If you’re passing more than one argument, you must be sure the number in that third parameter and the number of arguments being passed is the same, or the sky will fall down. No exaggeration.

There’s probably a better way to do this, but if so the documentation is scarce. It works, which is the main thing. If this helped you out, or if I’m doing it backwards, please leave a comment and let me know!

Categories: Tutorials

Comments
  • 1st March 2011

    I was looking for such solution! I really did’t thought for this solution. Also looked in the WP codex, doesn’t say anything. Thanks!

  • SPAMMENOT
    7th March 2011

    Thank you for the very interesting article.
    Since the “add_action” support anonymous function as callback parameter, what did you think about using the PHP create_function to accomplish the task?

    $argument = ‘something’;
    add_action(‘admin_init’, create_function(‘$argument’, ‘my_function($argument)’));

  • Max Bellasys
    8th November 2011

    I’m going to experiment with this- it might be a good fit for a function I’m working out- I happen to have Publish, Update, Edit & Save posts hitting the same function and in one instance (draft_to_publish) need an argument passed which will prevent the sky from falling.

    I think your suggestion might work since my argument needs to be based on “post_status” and so when the one instance is achieved (draft_to_publish), perhaps I can detect and pass the correct parameters to my main (space-saving) function.

    @SPAMMENOT- in my case this is exactly why I’d want this instead of create_function – to offer server-side scripting when post-data has changed or is in transit as my case suggests… just one of many possibilities.

    Thanks Michelle!

Leave a Reply

Your email address will not be published. Required fields are marked *

Error: Please enter a valid email address

Error: Invalid email

Error: Please enter your first name

Error: Please enter your last name

Error: Please enter a username

Error: Please enter a password

Error: Please confirm your password

Error: Password and password confirmation do not match