6

In my themes functions.php I trying to add a function with parameters (as a test if this works, not for the functionality) and it simply doesn't work.

The parameters always arrive empty even if I call do_action with the parameters enqueued as suggested in this codex page.

function alter_item ($user, $items, $action) {
    global $current_user, $menu;
    get_currentuserinfo();

    switch ($action) {
        case false:
            if ($current_user->user_login == $user) {
                remove_menu_page ($items);
            }
        break;

        case true:
            if ($current_user->user_login == $user) {
                remove_menu_page ($items);
            }
        break;
    }
}

add_action( 'admin_menu', 'alter_item', 10, 3 );
do_action('alter_item', 'my-user', 'plugins.php', false);
8

You are using it wrong.

  • add_action: attachs a function to an action hook. In your code, you are attaching alter_item funtion to admin_menu action hook. So, when admin_menu action happens, alter_item function is executed. According to codex, no parameters are passed to functions attached to admin_menu. So, the parameters your are trying to use in alter_item are not valid.

  • do_action: invokes all functions attached to an action hook. In your code, you are invoking all functions attached to alter_item action hook. alter_item action hook would be a custom action hook, as it is not in WP core, but currently in your code there is zero functions attached to this action, so nothing will happen with your do_action('alter_item'....

The correct way to attach a function to admin_menu is:

function alter_item() {
    //Do whatever you want
}
//The priority argument (10 the code bellow) is optional.
add_action( 'admin_menu', 'alter_item', 10 );

The correct way to define custom actions:

do_action('alter_item', 'my-user', 'plugins.php', false);

Then you can attach functions to alter_item action like this:

add_action( 'alter_item', 'alter_item_attached_function', 10, 3 );

function alter_item_attached_function( $user, $items, $action ) {
     //Now $user, $items and $action will be 'my-user', 'plugins.php' and false
}

If you want to pass information to core actions, you can:

  • use the valid parameters for each action. Refer to official documentation of each action.
  • define global variables, use options, transiets or custom objects properties/methods, so you can use that information in differents places of your code. Example.
  • Use PHP anonymous functions with use keyword.

Example using use keyword:

$user = 'my-user';
$items = 'plugins.php';
$action = false;

add_action( 'admin_menu', function() use ($user, $items, $action) {
    global $current_user, $menu;
    get_currentuserinfo();
    switch ($action) {
        case false:
            if ($current_user->user_login == $user) {
                remove_menu_page ($items);
            }
        break;

        case true:
            if ($current_user->user_login == $user) {
                remove_menu_page ($items);
            }
        break;
    }
} );
  • WOW that's much more complicated then I have imagined, but makes 100% sense, thanks cybmeta! – mebu83 Jan 15 '15 at 11:48
  • Downvoted, because anonymous function calls via action_hooks can't be unhooked. This is really bad practice, because it means others can't remove/override the call. Action hooks should ALWAYS be done properly. eg: add_action('some_action_hook', 'some_function'); // This is good and can be removed via remove_action(). add_action('admin_menu', function() { // stuff }); // this is bad, because now there's no way to use remove_action to remove this behavior. Really, this method is just lazy and sloppy and there's no real valid reason to ever need to use it, aside being lazy :) – Hybrid Web Dev Dec 11 '15 at 22:57
  • 1
    I disagree with you. There can be situations when the use of anonymous functions hooked to actions and filters can be totally justified. Also, you must know that anonymous functions can be unhooked (in a very dirty way I admit). Anyway, it was just one example of may possibilities I talked about (global variables, options, transients, object properties, .....). Read again the answer with more attention, not be lazy reading ;) – cybmeta Dec 12 '15 at 6:43

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.