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

user roles - wp_update_user not updating

programmeradmin5浏览0评论

In desperation I am asking for help in this forum too - if someone (ANYONE!) could take a look a this post and see if they could help I'd be eternally grateful

Basically - I am updating a user's role and capabilities via script but the changes only come in to effect (ie the user can see specific menu items) when I go into that user and hit "save"

Thanks in anticipation!!

Chris

UPDATE:

Maybe this helps solve it?

This is the WP_User object I have echoed out on screen when logged in as the newly changed user:

WP_User Object ( [data] => stdClass Object ( [ID] => 130 [user_login] => test [user_pass] => $P$BuHO1ABLCNQ716tktgyes4jqqkfVxG. [user_nicename] => test [user_email] => [email protected] [user_url] => [user_registered] => 2012-07-19 12:07:52 [user_activation_key] => [user_status] => 0 [display_name] => test ) [ID] => 130 [caps] => Array ( [editor] => 1 ) [cap_key] => wp_capabilities [roles] => Array ( [0] => editor ) [allcaps] => Array ( [upload_files] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [manage_options] => 1 [view_menu] => 1 [editor] => 1 ) [filter] => )

This is how the menu item is being created in my plugin file (which should display to the user):

add_menu_page('Welcome', 'Welcome','edit_posts', 'welcome', 'welcome_page', get_bloginfo('template_url').'/images/icon.png', 0);

This is the function / page that the menu item returns:

function welcome_page()
 {
    global $currrent_user;
if(!current_user_can('edit_posts'))
{

    print '<div class="wrap"><h2>Your account has been restricted, most likely due to an unpaid subscription.</div>';
}
else
{
    include 'welcome-page.php';
}
 }

As you can see - the user only needs edit_posts capability to view the menu item and for the function to return the welcome page. The user indeed has this capability, but cant do either of these things - unless i click "save" as admin in the user-edit page??

Edit 2

The following are all the different approaches I have taken to change the user role - if it helps!

//using this currently
$user = new WP_User($unpaid->uid);//$unpaid->uid is the users ID
$user->set_role('editor');
if(!$user->has_cap('edit_posts'))
{
$user->add_cap('edit_posts');
}
wp_cache_delete($unpaid->uid, 'users');

//another attempt
$uID = $unpaid->uid;
wp_insert_user(array('ID'=>$uID,'role'=>'editor')); 

UPDATE AGAIN!

I have just tried this and again, the db is updated, even shows 'editor' in the admin panel, but the user still cant see the appropriate menu items unless I click "update" on their profile!

$new = new WP_User($current_user->ID);
$new->set_role('editor');

wp_cache_delete( $new->ID, 'users' );
wp_cache_delete( $new->user_login, 'userlogins' );
wp_cache_delete( $new->user_email, 'useremail' );
wp_cache_delete( $new->user_nicename, 'userslugs' );
do_action('profile_update');

Any thoughts?

In desperation I am asking for help in this forum too - if someone (ANYONE!) could take a look a this post and see if they could help I'd be eternally grateful

http://wordpress/support/topic/wp_update_user-not-updating?replies=11

Basically - I am updating a user's role and capabilities via script but the changes only come in to effect (ie the user can see specific menu items) when I go into that user and hit "save"

Thanks in anticipation!!

Chris

UPDATE:

Maybe this helps solve it?

This is the WP_User object I have echoed out on screen when logged in as the newly changed user:

WP_User Object ( [data] => stdClass Object ( [ID] => 130 [user_login] => test [user_pass] => $P$BuHO1ABLCNQ716tktgyes4jqqkfVxG. [user_nicename] => test [user_email] => [email protected] [user_url] => [user_registered] => 2012-07-19 12:07:52 [user_activation_key] => [user_status] => 0 [display_name] => test ) [ID] => 130 [caps] => Array ( [editor] => 1 ) [cap_key] => wp_capabilities [roles] => Array ( [0] => editor ) [allcaps] => Array ( [upload_files] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [manage_options] => 1 [view_menu] => 1 [editor] => 1 ) [filter] => )

This is how the menu item is being created in my plugin file (which should display to the user):

add_menu_page('Welcome', 'Welcome','edit_posts', 'welcome', 'welcome_page', get_bloginfo('template_url').'/images/icon.png', 0);

This is the function / page that the menu item returns:

function welcome_page()
 {
    global $currrent_user;
if(!current_user_can('edit_posts'))
{

    print '<div class="wrap"><h2>Your account has been restricted, most likely due to an unpaid subscription.</div>';
}
else
{
    include 'welcome-page.php';
}
 }

As you can see - the user only needs edit_posts capability to view the menu item and for the function to return the welcome page. The user indeed has this capability, but cant do either of these things - unless i click "save" as admin in the user-edit page??

Edit 2

The following are all the different approaches I have taken to change the user role - if it helps!

//using this currently
$user = new WP_User($unpaid->uid);//$unpaid->uid is the users ID
$user->set_role('editor');
if(!$user->has_cap('edit_posts'))
{
$user->add_cap('edit_posts');
}
wp_cache_delete($unpaid->uid, 'users');

//another attempt
$uID = $unpaid->uid;
wp_insert_user(array('ID'=>$uID,'role'=>'editor')); 

UPDATE AGAIN!

I have just tried this and again, the db is updated, even shows 'editor' in the admin panel, but the user still cant see the appropriate menu items unless I click "update" on their profile!

$new = new WP_User($current_user->ID);
$new->set_role('editor');

wp_cache_delete( $new->ID, 'users' );
wp_cache_delete( $new->user_login, 'userlogins' );
wp_cache_delete( $new->user_email, 'useremail' );
wp_cache_delete( $new->user_nicename, 'userslugs' );
do_action('profile_update');

Any thoughts?

Share Improve this question edited Oct 12, 2012 at 1:12 Clearmedia asked Jul 15, 2012 at 4:13 ClearmediaClearmedia 1261 silver badge5 bronze badges 13
  • I have tried everything I know of here guys - any help would be much appreciated! – Clearmedia Commented Aug 7, 2012 at 1:17
  • Are you using another plugin such as User Role Editor by any chance? I'm looking at doing something similar to you but haven't got round to it yet. My early investigations have left me with a note to look into the wp_capabilities values. It's been a while so I can't remember why but, I've added this code snipped to my notes which could help you? update_user_meta($user_id, 'wp_capabilities', ''); – TomC Commented Nov 11, 2013 at 6:48
  • If the user flushes browser cache does this show the menu? Also if supercache etc are disabled, or both, does this help? – Tom J Nowell Commented Jan 20, 2014 at 1:10
  • Also does changing the capability required on the menu to the editor role rather than edit_posts help? – Tom J Nowell Commented Jan 20, 2014 at 1:13
  • 1 I have the exact same issue with a normal wp_user_update of the display_name. I disabled my caching solutions and nothing changes. If I go to the profile page of the user, the display name is updated, but the website shows it only after I hit the Update button in user-edit.php – Stefano Tombolini Commented Apr 11, 2018 at 13:26
 |  Show 8 more comments

3 Answers 3

Reset to default 4

Im using remove_role and then add_role to upgrade a user from one role to another.

Here is a function that checks all user within the user role subscriber and upgrade them to editor every hour.

/**
* Add a cron job that will update
* Subscribers to editors. Runs hourly
*/


add_action('check_user_role', 'upgrade_user');

function run_check_user_role() {
    if ( !wp_next_scheduled( 'check_user_role' ) ) {
        wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'check_user_role');
    }
}
add_action( 'wp', 'run_check_user_role' );



function upgrade_user() {

    // Get users in subscriber role
    $args = array(
        'role'  =>  'subscriber',
    );

    $users = get_users( $args );

    foreach ( $users as $user ) {

        $user = new WP_User( $user->ID );

        // Remove current subscriber role
        $user->remove_role( 'subscriber' );

        // Upgrade to editor role
        $user->add_role( 'editor' );

    }
}

Here is a way so you can try to upgrade the users manually:

Run this as http://yourdomain/?upgrade_user

if ( isset( $_REQUEST['upgrade_user'] ) ) {
    upgrade_user();
} 

I better method is. Which uses WP_User to toggle/set the role.

$role = 'editor';
$user = new WP_User($user_id);
$user->set_role($role);

If you use other plugins you need to read the documentation on how to set role. In my case, I used Ultimate Member so I have to add these lines:

UM()->roles()->set_role( $user_id, $user_role );
//and then clear the user cache after updating the role:
UM()->user()->remove_cache( $user_id );
发布评论

评论列表(0)

  1. 暂无评论