Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasidhda/6829ceaf8a2d12a9c0b15c021e1ba358 to your computer and use it in GitHub Desktop.
Save prasidhda/6829ceaf8a2d12a9c0b15c021e1ba358 to your computer and use it in GitHub Desktop.
add_action('wpmu_new_blog', 'wpmudev_new_blog_role', 300, 2);
function wpmudev_new_blog_role( $blog_id, $user_id ) {
//Switch to new network site
switch_to_blog( $blog_id );
//Set default fallback user role
$default_user_role = 'administrator'; // Will be used as fallback user role, will be used if WP somehow can't create user role programitically
//Create free user role
$free_user_role = add_role( 'free', 'Free', array('delete_others_pages') );
//Set default user role to Free only if it is successfully created
if( null != $free_user_role ){
$default_user_role = 'free';
}
//If it is super admin
if( is_super_admin( $user_id ) ){
//first create the Master user role and then
$master_user_role = add_role( 'master', 'Master', array('setup_network') );
////Set default user role to Master only if it is successfully created
if( null != $master_user_role ){
$default_user_role = 'master';
}
}
$user = new WP_User( $user_id );
$user->set_role( $default_user_role );
restore_current_blog();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment