[英]Change Wordpress user role on gravityforms submission
I am using Gravityforms along with User registration add-on and have a form which when submitted should change the role of the current user to a new role without any underlying conditions.我正在使用 Gravityforms 和用户注册附加组件,并且有一个表单,提交时应该将当前用户的角色更改为没有任何基础条件的新角色。
Using the gravity\\forms docs https://docs.gravityforms.com/gform_user_updated/#1-update-user-role and trying this:使用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
}
But its not working!但它不工作! Does anyone have any idea why this is incorrect or any other modifications to the code?
有谁知道为什么这是不正确的或对代码的任何其他修改?
There's a couple things I see with your code when comparing it to the Gravity Forms doc.在将您的代码与 Gravity Forms 文档进行比较时,我看到了一些事情。
Here's your code with some added comments:这是您的代码,并添加了一些注释:
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
}
I think you can simplify it some.我认为你可以简化一些。 Try this instead:
试试这个:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.