• Resolved askwpcoach

    (@askwpcoach)


    Hello,

    When a user registers, I am first setting their role to Editor, but once my plugin activates, I need to change their role to Administrator.

    Below is my current code:

    function ap_change_role() {
    	global $wpdb;
    	$blog_id = get_current_blog_id();
    	$query = $wpdb->prepare("
    		SELECT 'user_id'
            FROM '%susermeta'
            WHERE ( meta_key LIKE 'primary_blog'
    			AND meta_value LIKE %d )
            LIMIT 1", $wpdb->base_prefix, $blog_id);
    
    	$blog_owner_id = $wpdb->get_var( $query );
    
    	$user = new WP_User( $blog_owner_id );
    	// Remove role
    	$user->remove_role( 'editor' );
    	// Add role
    	$user->add_role( 'administrator' );
    
    }
    register_activation_hook( __FILE__, 'ap_change_role');

    But the site owners role is not changing after activation.

    Any help with getting this to work will be much appreciated.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Given that I’m pretty sure they can’t be the blog owner while not being an admin, that may be what’s going on…

    Why aren’t they just admins from the start?

    Thread Starter askwpcoach

    (@askwpcoach)

    Yes, they are the blog owner – but they are not admin from the start because they have limited functionality in the beginning, which I have only been able to achieve by starting them off as editor, or I would have to assign them custom capabilities.

    Either approach (change role / change capability) – I need to do it after the blog owner upgrades.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    But… WHY?

    This really does matter ?? Is there a specific ‘thing’ that has to occur for you to go “AHA! You are now the admin!” Maybe we should trigger that instead.

    Thread Starter askwpcoach

    (@askwpcoach)

    My project has two types of members – free & paid – I was starting the member off as Editor, because I needed to restrict access to certain plugins which should only come available after upgrade.

    These plugins I want to restrict access to, come available for all Administrator role users and when I was trying to regulate with capabilities, sometimes I had problems.

    Since your questioning :-), I have given more thought and decided to go back to trying to regulate all access by changing the user capabilities only.

    I am no longer going to start members off as Editor then change role to Administrator after upgrade.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change user role on plugin activation’ is closed to new replies.