0

I wanted to pass arguments to a function triggered by an add_action hook. I followed the codex on this. I got a warning and the result was erratic, with an unexpected behavior.

This question has already been adressed before without answers.

I could reproduce this problem with a simplified function echoing the 2 arguments it needs. So, in functions.php:

function my_function ($arg1, $arg2) {
    echo 'id1: '.$id1.' id2: '.$id2;
    return;
}
add_action('this_hook', 'my_function', 10, 2);

The 2 number stands for the number of arguments needed by my_function in order it can take into account some of its context when executed.

So, to be executed, the do_action must be added in the template pages files, for example in header.php as it was the case for me.

 do_action('this_hook', 'dogs', 'cats');

The result is echoing

id1: id2: id1: dogs id2: cats Missing argument 2 for my_function()

The expected behavior would have been:

id1: dogs id2: cats

  • 2
    You realize that your function name is add_custom_submenu but you are using add_bp_custom_submenu as the callback, right? – s_ha_dum May 15 '14 at 3:09
  • Thanks for having taken a look to my question. I verify very carefully (it is driving me crazy :) : but the code is ok with the same function name, it's only when writing the question i made the mistake. I apologize and edit the question to correct it. Please take into account the problem is not from the function name. Here is a gist if needed : gist.github.com/Nandrine/9d2083c5978c5c229ae0 – Sandrine May 15 '14 at 9:16
  • @s_ha_dum I don't understand why this question has been marked as off-topic. I now have the answer and I'd like to share it if I can? The problem I met can be reproduced when hooking in whatever hook that has already been triggered by a do_action in the core files. In that particular case, demanding arguments when registering a new function within an add_action('hook','function', $priority, $nb_arguments) will have as consequence to issue this warning, as well as another side effects. I can explain how to avoid this too if it interests somebody. – Sandrine May 16 '14 at 15:47
  • Rewrite/edit so that the question is more general (not so plugin specific) and it can be reopened. – s_ha_dum May 16 '14 at 15:52
  • I've rewritten the question. Just to explain, when i was confronted to the problem, this_hook was a hook of buddypress which name is bp_setup_nav used to build the main menu. Thanks to the explanations I received here, I was able to find that this hook was already used elsewhere. But the problem is not specific to the buddypress plugin, but to the hooks mechanism. – Sandrine May 16 '14 at 17:39
2

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');
  • 1
    +1, but I would argue that you should not be firing action hooks except in the places they are fired in the Core or in the application providing the hook. It seems like a very good way to get unexpected behavior. The same is probably true of some filters. – s_ha_dum May 16 '14 at 18:55
  • Indeed. Do you mean that we should never add do_action on an existing hook in the template files? In my case, I wanted to modify the menu by calling this hook, which is OK until I began to think it would be great that this can be done dynamically, by passing arguments taking account some context variables. The prefered solution (not to access global variables) usually mentionned is to add a do_action in a template file with the arguments. I followed this way without being aware the consequences! Passing arguments to a function through add_action seems to be very tricky. – Sandrine May 16 '14 at 19:49
  • This helped point me in the direction. I added 10,99 to my two hooks so they execute later. thank you. – Jacob Raccuia Aug 10 '16 at 21:39
0

From your Gist you register your hook like so:

add_action( 'bp_setup_nav', 'add_bp_custom_submenu', 10, 2 );

...but you try to fire it by the function name:

do_action( 'add_bp_custom_submenu', 213, 'dashboard' );

...when you should be firing by the hook name:

do_action( 'bp_setup_nav', 213, 'dashboard' );
  • Yes, i dit it like this first, with the hook_action_name 'bp_setup_nav', with the same result. I changed it (and it remains in the gist - corrected since thanks) to test as I can't understand how WP could know wich function to trigger, when there are several ones pluggued into the same hook. – Sandrine May 15 '14 at 12:00
  • I can tell something else, like the other person mentionning the problem here wordpress.org/support/topic/…, the bp_nav_menu I'm trying to modify is repeated twice (without being modified by the function of course). – Sandrine May 15 '14 at 12:07
  • I updated the gist with more details. I tried to investigate why bp_nav_menu has been excuted twice. I print the bp_nav_menu content before and after the do_action. You'll see how it is duplicated, and it seems that the function 'add_bp_custom_submenu' HAS been executed ONCE despite of the warning. Please visit the gist : gist.github.com/Nandrine/9d2083c5978c5c229ae0 – Sandrine May 15 '14 at 12:49
  • I think there's some misunderstanding as to how you're implementing hooks. I would go back to basics and simply make the two functions echo a unique string. Fire the hook, check your output. Now things should be making sense. – TheDeadMedic May 15 '14 at 13:36
  • OK, I'm sure i'm doing all wrong! I simplify as you suggested (one function with two parameters, same hook in add_action and do_action in header.php) and put it in another gist. gist.github.com/Nandrine/31e261579559c82de5eb Results are the same, executed twice, one with the warning, one OK – Sandrine May 15 '14 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.