Add user to main site

When a user create a blog with Pro Sites it's added as network user but not in the main site. It's possible to add the user as a subscriber on the main site, when it creates a site using Pro Sites?

  • Konstantinos Xenos

    Hi Seb !

    Could you please add this code on your functions.php ( preferably your child-themes ), this should add any user that successfully created his own blog to your main site as a subscriber as well.

    ( Always make sure to keep a backup before changing / adding custom code to your site! )

    add_action( 'wpmu_new_blog', 'add_new_blogadmin_on_main_site', 10, 6 );
    function add_new_blogadmin_on_main_site( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
    	add_user_to_blog( get_network()->site_id, $user_id, 'subscriber' );
    }

    Regards,
    Konstantinos

  • Konstantinos Xenos

    Hey Seb !

    Yes that's normal since by being subscribers they still have some 'limited' access to the sites information and one of them is the Dashboard ( each plugin should choose though what information to add on any user level ).

    In any case you could try adding these 2 functions as well:

    The 'dashboard_access_rights' will redirect any user that is a subscriber to view the front-page when they try to go into the dashboard so pretty much you're cutting them off with this so they're not going to be able to access it even.

    The 'hide_main_site_from_menus' adds a little jquery snippet on both the admin area + the front-end of the site and 'removes' the 1st website that is under the My Sites list as well as the 1st block on the My Sites list page. Since the 'main site' is always the first one it should practically be 'hidden'. Now this is just for a 'visual' trick as the redirect function is already implemented to just avoid confusion as much as possible.

    I'm attaching 2 screenshots as well 1 from my super admin that sees all the sites and 1 from a Pro site admin that owns 2 subsites and is subscribed to the main so you can see the difference.

    On the right side you can see the 'testerino' account that is a subscriber on the main site as well but can't see it on any of the lists.

    add_action( 'admin_init', 'dashboard_access_rights' );
    function dashboard_access_rights() {
    	if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
    		$user            = get_userdata( get_current_user_id() );
    		$caps            = ( is_object( $user ) ) ? array_keys( $user->allcaps ) : array();
    		$block_access_to = array( 'subscriber' );
    		if ( array_intersect( $block_access_to, $caps ) ) {
    			wp_redirect( home_url() );
    			exit;
    		}
    	}
    }
    
    add_action( 'admin_footer', 'hide_main_site_from_menus' );
    add_action( 'wp_footer', 'hide_main_site_from_menus' );
    function hide_main_site_from_menus() {
    	if ( is_user_logged_in() && ! is_super_admin() ) {
    		?>
    		<script>
    			( function( $ ) {
    				$( document ).ready( function() {
    					$( 'li#wp-admin-bar-blog-1' ).remove();
    					$( 'ul.my-sites li:first-child' ).remove();
    				} );
    			} ( jQuery ) );
    		</script>
    		<?php
    	}
    }

    Tell me if this helps as cutting of access is a bit tricky ( and as always make sure to keep a backup first before doing changes ) !

    Regards,
    Konstantinos

  • Konstantinos Xenos

    Ah! The 'View Site menu' you mean probably. That was left out on purpose since the dashboard is locked either way. I didn't know you wanted all of the menus to be hidden.

    Could you replace the 2nd function with this instead? This will hide the menu if the users are viewing the 'main site' only so on their own the menu will be shown properly for them to access it.

    add_action( 'admin_footer', 'hide_main_site_from_menus' );
    add_action( 'wp_footer', 'hide_main_site_from_menus' );
    function hide_main_site_from_menus() {
    	if ( is_user_logged_in() && ! is_super_admin() ) {
    		$content = '<script>(function($){$(document).ready(function(){$(\'li#wp-admin-bar-blog-1\').remove();$(\'ul.my-sites li:first-child\').remove();';
    
    			if ( is_main_site() ) {
    			$content .= '$(\'#wp-admin-bar-site-name\').remove();';
    		}
    
    		$content .= '});}(jQuery));</script>';
    	}
    
    	echo $content;
    }

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.