Forums

Remove role, then replaced with ...? (6 posts)

  1. leepettijohn
    Member
    Posted 1 year ago #

    When you use the remove_role function, what is that users role replaced with?

  2. Big Bagel
    Member
    Posted 1 year ago #

    Nothing. If you remove "subscriber", for example, it won't automatically be replaced with anything similar unless you use add_role(). Or are you asking about removing a role that a user is currently a part of?

  3. leepettijohn
    Member
    Posted 1 year ago #

    Yes, if I add plugin that has a role called "New Admin Role" and then change all administrators to that role. When I remove the plugin, and thus, remove the "New Admin Role", what role replaces that?

    Does WordPress remember the last role registered and change to that?

  4. Big Bagel
    Member
    Posted 1 year ago #

    No idea; I'm not sure how new roles are stored. It might remember the new role after you delete the plugin, or it might put them in a pseudo-subscriber role, where they have the same abilities as a subscriber but no actual role assigned. The best way to know would be to test it: Install the plugin, make a test user, assign it a unique role, then remove the plugin.

    Edit: You could always ask the plugin author or post a topic through that plugin's page.

  5. leepettijohn
    Member
    Posted 1 year ago #

    Just tested this and if you remove the role, it sets the user to "-No Role For This Site-"

  6. leepettijohn
    Member
    Posted 1 year ago #

    BUT ...

    Utilizing this code ...

    $roles_to_delete = array('role1','role2','role3');
    
    foreach ( $roles_to_delete as $role ){
    	$users = get_users( array( 'role' => $role ) );
    	if ( count( $users ) <= 0 ){
    		remove_role( $role );
    	}
    	else {
    		foreach ($users as $user){
    			$userid = $user->ID;
    			$user_id_role = new WP_User($userid);
    			$user_id_role->set_role(get_option('default_role'));
    		}
    		remove_role( $role );
    	}
    }

    This will scroll through each user of the roles you want to delete and set them to your "default role." Thereby, helping you clean up your plugin when deleted.

Topic Closed

This topic has been closed to new replies.

About this Topic