最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Updating wp_user_level on user update

programmeradmin3浏览0评论

I have the following code, which properly adds the wp_user_level of 2 when a new user is registered with the role "division_manager". I need the wp_user_level to be non-zero so they can be the author of posts.

add_action( 'user_register', 'set_user_default_division_manager', 10, 1 );

function set_user_default_division_manager( $user_id ) {
    $user = new WP_User( $user_id );

    foreach( $user->roles as $role ) {
        if( $role === 'division_manager' ) {
            update_user_meta( $user_id, 'wp_user_level', 2 );
        }
    }
}

I'm trying to work out code that will add the wp_user_level when an existing user is given the role of "division_manager". This is what I have, but the user level only updates when I save the users profile twice. I've also tried the actions: 'personal_options_update' & 'edit_user_profile_update'.

add_filter( 'insert_user_meta', 'update_division_manager_user_level', 10, 1 );

function update_division_manager_user_level( $user_id ) {   
    foreach( $_POST['members_user_roles'] as $role ) {
        if( $role === 'division_manager' ) {
            update_user_meta( $user_id, 'wp_user_level', 2 );
        }
    }
}

Any idea what I'm missing?

发布评论

评论列表(0)

  1. 暂无评论