我正在使用Gravityforms和用户注册附加组件,并有一个表单,当提交时,应该改变当前用户的角色到一个新的角色,没有任何潜在的条件。
使用gravity\forms docs https://docs.gravityforms.com/gform_user_updated/#1-update-user-role并尝试执行以下操作:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // update 'role' to the name of the desired role
} 但是它不起作用!有谁知道为什么这是不正确的,或者对代码进行了任何其他修改吗?
发布于 2018-02-01 05:53:47
当我将你的代码与Gravity文档进行比较时,我看到了一些东西。
以下是添加了一些注释的代码:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user; // you probably don't need this
get_currentuserinfo(); // you probably don't need this
$user_id = $current_user->ID; // $user_id should already be a numeric value passed in to the function containing the logged in user's ID so you shouldn't need to do this. You're resetting the $user_id variable here to whatever is being pulled out of the get_currentuserinfo() function, and I'm guessing that's the problem
//I would get rid of this echo and if statement
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // the word "role" here needs to be the role name
}我想你可以把它简化一些。试着这样做:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
$user = new WP_User( $user_id );
$user->set_role( 'new_role_name_here' ); // Add an existing role here to update the user too
}https://stackoverflow.com/questions/48551311
复制相似问题