0

I have a general question about functions and add_action to it.

I have a function and adding an action do it. But how can i define a value for the argument on this action?

add_action( 'um_members_just_after_name', 'my_members_after_user_name', 10, 1 );
function my_members_after_user_name( $user_id ) {

    // how can i set a value for $user_id ???

    echo '<i class="um-verified um-icon-checkmark-circled um-tip-s"></i>';
}

Sorry, it may be a stupid question but i dont get the point for it... Normally in php it looks like this and i understand it here

function Multiplication($x, $y) {
    $z = $x * $y;
    return $z;
}
     echo "5 * 10 = " . Multiplication(5, 10) . "<br>";
     echo "7 * 13 = " . Multiplication(7, 13) . "<br>";
     echo "2 * 4 = " . Multiplication(2, 4);
0

WordPress core and third-party plugins can trigger events during the execution of their respective code. When you call add_action on um_members_just_after_name, you're saying: Whenever this um_members_just_after_name action runs, go ahead and run my function my_members_after_user_name, too. That said, $user_id must be supplied by um_members_just_after_name, in other words, if um_members_just_after_name doesn't exist or doesn't supply $user_id, it will never be set-- it's not something you can set unless you are the creator of the um_members_just_after_name action.

  • Thanks for explanation... So i can say, if $user_id = 2 then do the action? – LovinQuaQua Mar 29 '19 at 7:54
  • No, it's not something you can set. It would already need to be passed in by the hook. – NightHawk Mar 29 '19 at 15:10
0

Building off of what NightHawk posted, actions do not normally allow you to change the value of the parameter passed to it, but filters do. Read more about actions and filters in WordPress.

I think it might be helpful to talk about the other side of add_action which is do_action.

Do and Add Action

For actions, a do_action is added to code in a plugin or in WordPress core to allow you to run your own function at particular points in the process. Developers usually refer to this as a hook since you're hooking into a process you normally wouldn't have access to.

You use add_action to tell that do_action that you'd like it to run your function.

Priority

The 10 in your code specifies the order in which it should run compared to other places where add_action is using that same do_action hook.

Number of Parameters

The final parameter (1 in your code) specifies the number of parameters you're expecting. This number cannot be more than the number of parameters in do_action. For example, do_action( 'um_members_just_after_name', $user_id ); will only ever pass over $user_id no matter how many parameters you want. On the other hand, do_action( 'um_members_just_after_name', $user_id, $user, $id ); will pass over 3 parameters, so you can specify 1, 2, or 3 and have those available in your function.

Note: the do_action must specify these parameters are available for you to make use of them in your function.

0

It seems you want to fire this action for certain user or users.
Try this

add_action( 'um_members_just_after_name', 'my_members_after_user_name', 10, 1 );
function my_members_after_user_name( $user_id ) {

    // how can i set a value for $user_id ???
    $user_ids =array(120, 220, 320, 420); // set values here
    if ( in_array($user_id, $user_ids ) ){


       // do something here

       echo '<i class="um-verified um-icon-checkmark-circled um-tip-s"></i>';
     }

}

I hope this helps.

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.