首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改gravityforms提交的Wordpress用户角色

更改gravityforms提交的Wordpress用户角色
EN

Stack Overflow用户
提问于 2018-02-01 04:58:42
回答 1查看 802关注 0票数 0

我正在使用Gravityforms和用户注册附加组件,并有一个表单,当提交时,应该改变当前用户的角色到一个新的角色,没有任何潜在的条件。

使用gravity\forms docs https://docs.gravityforms.com/gform_user_updated/#1-update-user-role并尝试执行以下操作:

代码语言:javascript
复制
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

}   

但是它不起作用!有谁知道为什么这是不正确的,或者对代码进行了任何其他修改吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-01 05:53:47

当我将你的代码与Gravity文档进行比较时,我看到了一些东西。

以下是添加了一些注释的代码:

代码语言:javascript
复制
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

}

我想你可以把它简化一些。试着这样做:

代码语言:javascript
复制
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

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48551311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档