WordPress.org

Support

Support » How-To and Troubleshooting » wp_update_user not updating

wp_update_user not updating

  • THE OVERVIEW

    I am using wp_update_user() in a plugin I have written. I have added a couple of new menu items that should only be visible if the user has the capability “edit_posts” or above.

    When a new user is created (via a plugin), they are given the role “subscriber” which means they cant access / view these other menu items.

    A script that runs daily uses the following to change users to “editor” role (and therefore the ability to see the menu items):

    wp_update_user(array('ID'=>$uid,'role'=>'editor'));

    THE PROBLEM

    In the DB, the wp_usermeta fields have been updated correctly: (i think?)

    wp_capabilities = a:1:{s:6:"editor";s:1:"1";}
    wp_user_level = 7

    BUT

    1. When the user logs in, they still cannot see the menu items
    2. Viewing the user in the admin area indeed says they are an “editor”
    3. If (as admin) I open the user profile and simply click “save”, everything works as expected and I cant for the life of me see any DB changes after the fact?

    Can someone please help with this? I’m sure it is something trivial

    Cheers
    Chris
    [ Please do not bump, it’s not permitted here. ]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Chris,

    Did you try to use wp_insert_user instead of wp_update_user?

    Alex

    Thanks for the repsonse Alex – unfortunately no, I used wp_update_user()

    Its quite strange – the update user seems to work but the user is unable to access according to their new permissions unless I go into the user in the admin and click save…? odd

    wp_insert_user function calls ‘profile_update’ hook when updated an existing user…which may fix it.

    Ahh ok – so I can simply replace update with insert and the function will update the user if already exists?

    Yes, try that.

    Ok – cross your fingers! And thanks for the help – I’ll keep you posted (pardon the pun) 🙂

    Wil

    @gravitationalfx

    Yup – @njalex1 is correct.

    Use wp_insert_user when creating a new user in the DB.

    Use wp_update_user if you need to change the existing users password.

    Hi Wil – thanks for the response

    I am actually updating a user’s credentials – not inserting a new user.

    I have written a plugin that creates a new user as a “subscriber”
    I have a cron job that runs a script every morning that:

    1. If the user has paid their invoice, it updates the user to “editor”
    2. Checks for unpaid invoices and if the due date is past todays date it updates the user to “subscriber” if they are an “editor”

    The problem is:

    When the user is updated to “editor”, they are still unable to access the menu items I have enabled for “editor” – unless I go into that user in the admin and click “save”?

    UPDATE:

    This is the offending code:

    $uID = MY USER'S ID
    $result = wp_insert_user(array('ID'=>$uID,'role'=>'editor'));
    if(is_wp_error($result))
    {
    	echo $result->get_error_message();
    }

    This is returning “Cannot create a user with an empty login name” WP_error

    As you can imagine – this is really bugging me – it’s the last piece of the puzzle!!!

    UPDATE #2

    I have also tried the following to no avail:

    $user = new WP_User($unpaid->uid);
    $user->set_role('editor');
    if(!$user->has_cap('edit_posts'))
    {
        $user->add_cap('edit_posts');
    }

    (where my menu items are available to users with “edit_posts” capabilites

    I have tried the following and the problem still exists:

    $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?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘wp_update_user not updating’ is closed to new replies.
Skip to toolbar