actions - add_action where function has arguments

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 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);

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);
Share Improve this question asked Mar 28, 2019 at 15:13 LovinQuaQuaLovinQuaQua 833 silver badges18 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646210a4637987.html

相关推荐

  • actions - add_action where function has arguments

    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

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信