• Jon

    (@freshyjon)


    I am using Ultimate Member with a registration form. The Ultimate Member plugin is set to “change the role” to a specific Role when a user registers. However, my goal is to also use User Role Editor to assign a secondary Role upon registration as well.

    Right now, the default Role is set to Subscriber. But when someone signs up with the Ultimate Member form, they become a custom Role… which is fine. But is there some sort of setting or function that I can add, that will ALSO assign an additional Role (since User Role Editor) allows that? Ideally, when someone signs up, they get the Ultimate Member role AND a secondary role that I specify.

    Update: I should mention that it appears Ultimate Member is overriding the “default” roles. So even if I tell User Role Editor to give a “default” user 2 different roles, the Ultimate Member form is not applying those. It’s applying its own role.

    • This topic was modified 7 years, 2 months ago by Jon.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Jon

    (@freshyjon)

    Or is there maybe a PHP function that I can set up on a 2 minute Cron, that will automatically check all users… to ensure they have a secondary role of Subscriber? Without overriding any previous role they may already have?

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Ultimate Member plugin assigns default role to a new user via this hook:

    
    add_action('um_after_new_user_register', 'um_after_new_user_register', 10, 2);
    

    and rewrite the roles of new user:

    
    ...
    $ultimatemember->user->set_role( $role );
    ...
    

    You can add your own routine with lower priority, like

    
    add_action('um_after_new_user_register', 'custom_after_new_user_register', 20, 1);
    function custom_after_new_user_register($user_id) {
      $user = get_user_by('id', $user_id);
      if (!in_array('subscriber', $user->roles)) {
        $user->add_role('subscriber');
      }
    }
    

    WordPress will execute it after UM and new user will receive the additional role.

    I did not tested this code, wrote it from scratch, just to give you an idea.

    Thread Starter Jon

    (@freshyjon)

    Unfortunately that did not seem to work. I realize it was just an example, but I’m not sure how to get that to actually work as intended.

    Is there any way to run a check to automatically assign a secondary role to all users, if it doesn’t exist? Maybe on login, or on an hourly basis?

    Just trying to figure out the best way to ensure all users ALWAYS have at least the role of Subscriber. Eeven if they have already have another role, they should still have Subscriber role.

    Thread Starter Jon

    (@freshyjon)

    Any additional assistance you could provide, to always check if a user has a specific role (e.g., Subscriber) and if they don’t have that role for their user … automatically add it as an additional role (while maintaining any other existing roles they have).

    • This reply was modified 7 years, 2 months ago by Jon.
    Plugin Author Vladimir Garagulya

    (@shinephp)

    This code is executed after user logged-in to WordPress:

    
    add_action('wp_login', 'add_subscriber_on_login', 10, 2);
    
    function add_subscriber_on_login($login, $user) {
    
        if (!in_array('administrator', $user->roles) &&
            !in_array('subscriber', $user->roles)) {
            $user->add_role('subscriber');
        }
    
    }
    
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Assign role on registration?’ is closed to new replies.