Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.org/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@gmail.com [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
    
I have tried everything I know of here guys - any help would be much appreciated! –  Clearmedia Aug 7 '12 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 Nov 11 '13 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 Jan 20 at 1:10
    
Also does changing the capability required on the menu to the editor role rather than edit_posts help? –  Tom J Nowell Jan 20 at 1:13

2 Answers 2

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);
share|improve this answer
1  
Since this has been -3'ed, can someone advise why my solution is wrong? –  Barry Carlyon Jul 15 '12 at 17:17
    
Thanks for the responses guys - have tried both these approaches but to no avail :( I believe the system is updating the database correctly but the user still can't access the applicable menu items until I go into that user and click save...? –  Clearmedia Jul 17 '12 at 22:32
    
Try adding wp_cache_delete($user_id, 'users'); after my $user->set_role. But before doing that. Does the user get shown the correct menu items, if they logout and in again? If so the WP temp cache is interfering and wp_cache_delete will clear this. (Have not tested) –  Barry Carlyon Jul 17 '12 at 22:53
    
Thanks Barry - they still cant see the menu items after logout and log back in again. - it's like the updating function is missing or not doing one small something that occurs when updating a user profile via the admin > users panel...? –  Clearmedia Jul 18 '12 at 6:09
1  
This answer is correct, I tested it, it works properly. I don't know what you're doing wrong, but there is literally nothing more to setting new roles than this. –  Otto Aug 26 '12 at 14:31

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.com/?upgrade_user

if ( isset( $_REQUEST['upgrade_user'] ) ) {
    upgrade_user();
} 
share|improve this answer
    
Tried that one Pontus - no good im afraid - my mind is slowly turning to mush - this is the one tiny fly in an otherwise cracking system! –  Clearmedia Jul 18 '12 at 6:12
    
Thats weird, im running this on a newly created WordPress 3.4 install and it works for me. Where do you add it? and do you have something like this function already? –  Pontus Abrahamsson Jul 18 '12 at 11:37
    
You can see my update above - I run the code in my plugin file. The database updates correctly - but the user still cant see the menu items, unless I log in as admin, go to users > edit the user and simply click "save"... –  Clearmedia Jul 19 '12 at 4:43
    
You should add global $current_user to check the capabilities of the user, i have updated your code. you can check on a role instead like this: if ( $user->has_cap('edit_posts') ) {} –  Pontus Abrahamsson Jul 19 '12 at 9:00
    
Sorry Pontus - you've updated my code? I have included another edit to my initial post - I have tried has_cap. Am I wrong in assuming I don't need to declare global $current_user if I already have the user's ID? –  Clearmedia Jul 19 '12 at 11:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.