missing argument 2 when passing arguments to add_action

This problem can be met when hooking in whatever hook that has already been triggered by a precedent do_action (ie. when this_hook has already been mentionned in a do_action within the core files of wordpress or within whatever plugin files)

In that particular case, demanding arguments when registering a new function within an add_action('this_hook','function', $priority, $nb_arguments) could have as possible consequence to issue this warning, as well as another side effects, like the repetition mentionned.

To explain, let’s say this_hook has been triggered twice like this:

  • in a core or plugin file: do_action( 'this_hook' );
  • by the developer in a template page file: do_action( 'this_hook', 'dogs', 'cats');

So, if you meet erratic behavior when passing arguments to add_action, you should verify if it is used in another place in the code (with a different number of arguments). Then, of course, as you don’t want to modify the core o plugin files to add the two arguments saying there are missing, one solution is to trigger the “registering” of the add_action on another localized hook in the template.

Let’s imagine that there are other hooks localized in the file header.php, lets say something like do_action( 'before_header') and another one do_action('after_header')

Then in functions.php:

function trigger_function () {
    add_action( 'this_hook', 'my_function', 10, 2);
}
add_action( 'before_header', 'trigger_function');

function remove_function () {
    remove_action( 'this_hook', 'my_function');
}
add_action( 'after_header', 'remove_function');

In header.php, you should have something like this:

do_action('before_header');
....
do_action('this_hook', 'dogs', 'cats');
....
do_action('after_header');