How to update user role without logout

I am working on a wordpress project with the following plugins :

  • Woocommerce
  • Woocommerce product vendors
  • WP Job Manager
  • Wp Job Manager products

I am trying to upgrade the user role after adding a job, so the user can access the wp-admin and edit his own product.

Now the user can be upgraded to Manage his Vendor dashboard but the problem that the first time he adds a Job he must login/logout in order to refresh his roles and be able to access the Dashboard.

Here what I tried :

        $current_user = wp_get_current_user();

        //Code 1 : 
        $user_id = wp_update_user( array( 'ID' => $current_user->ID, 'role' => 'wc_product_vendors_manager_vendor' ) );

        //Code 2 : 
        $user = new WP_User( $current_user->ID );
        $user->remove_role( 'customer' );
        $user->set_role( 'wc_product_vendors_manager_vendor' );

        //Code 3 : ( this will make the user with 2 roles )
        $current_user->add_role( 'wc_product_vendors_manager_vendor' );

Is it possible to achieve this by deleting the wp_cache_delete … does anyone knows a good solution to upgrade user roles without login/logout ?

Thank you for you help!

2 Answers
2

I think you are on the right track, wp_cache_delete was what finally helped me get an auto-signup with auto-login plugin working… I have this from there:

wp_cache_delete($current_user->ID, 'users');
wp_cache_delete($current_user->user_login, 'userlogins');

Then see what roles you get after that with:

$current_user = wp_get_current_user();

Leave a Comment