4

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!

3

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();
| improve this answer | |
  • It works but I got some security issue, for some reason the user got some access to the hole page in the administration ( he can see all the vendors products instead of only his own product ) so for now I just changed the hole logic instead of updating the user role, I am trying to add the role when he first signup, so when he just login he will got the right roles already. Thanks for your help. – Mohamed Salem Lamiri May 17 '16 at 11:36
  • ok that is a problem, if you want to keep testing, I'd suggest adding the code from @Shane's answer to after the cache delete code to try logging the user in again properly. – majick May 18 '16 at 5:41
2

Have you tried logging in the user after your changes?

Such as:

 wp_set_current_user( $current_user->ID, $current_user->user_login );

 wp_set_auth_cookie( $current_user->ID );

 do_action( 'wp_login', $current_user->user_login );
| improve this answer | |
  • Yep I just tried this and it throws some errors from the theme and the plugin Warning: Missing argument 2 for WC_Product_Vendors_Authentication::vendor_login_successful() .... ( Argument 2 is the user login ...) – Mohamed Salem Lamiri May 11 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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