WordPress.org

Ready to get started?Download WordPress

Forums

wp_update_user not updating (12 posts)

  1. Clearmedia
    Member
    Posted 2 years ago #

    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. ]

  2. njalex1
    Member
    Posted 1 year ago #

    Chris,

    Did you try to use wp_insert_user instead of wp_update_user?

    Alex

  3. Clearmedia
    Member
    Posted 1 year ago #

    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

  4. njalex1
    Member
    Posted 1 year ago #

    wp_insert_user function calls 'profile_update' hook when updated an existing user...which may fix it.

  5. Clearmedia
    Member
    Posted 1 year ago #

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

  6. njalex1
    Member
    Posted 1 year ago #

    Yes, try that.

  7. Clearmedia
    Member
    Posted 1 year ago #

    Ok - cross your fingers! And thanks for the help - I'll keep you posted (pardon the pun) :)

  8. Wil
    Member
    Posted 1 year ago #

    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.

  9. Clearmedia
    Member
    Posted 1 year ago #

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

  10. Clearmedia
    Member
    Posted 1 year ago #

    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!!!

  11. Clearmedia
    Member
    Posted 1 year ago #

    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

  12. Clearmedia
    Member
    Posted 1 year ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags