How to display only the user registered blogs and not the main website

Hi,

I have a Multisite website (https://iapps2u.xyz) where I do not restrict the number of blogs that can be registered with each email account. Each time when the user goes to the wp-signup.php page, it will give a message that says "Sites you are already a member of:" and it will list all the sites that user has registered, inclusive of my main website https://iapps2u.xyz. I do not want the main website https://iapps2u.xyz to be listed because it is confusing to my users. Can you advise how I can disable the main website from being listed?

Thank you
Esther

  • Predrag Dubajic

    Hey Esther,

    Hope you're doing well today :slight_smile:

    You can use below code and add it either to functions.php of a child theme used on your main site or as a mu-plugin:

    <?php
    function remove_main_blog_from_get_blogs($blogs) {
    	// check that user is not Network Admin
    	if ( !is_super_admin() ) {
    		// remove first item from $blogs array, which should always be main blog
    		array_splice($blogs, 0, 1);
    	}
    	return $blogs;
    }
    add_filter( 'get_blogs_of_user', 'remove_main_blog_from_get_blogs' );
    ?>

    After that your signup page will hide main site from the list for all users that don't have super admin privileges.

    Let us know if that's what you're looking for :slight_smile:

    Best regards,
    Predrag

  • Adam Czajczyk

    Hello Esther!

    If you were to use the code as an MU plugin, you would want to use it in that exact form that my colleague Predrag shared. However, if it's used inside "functions.php" file, the PHP opening and closing tags should be removed. This would be the same code but in a versions to be used inside the "functions.php" file:

    function remove_main_blog_from_get_blogs($blogs) {
    	// check that user is not Network Admin
    	if ( !is_super_admin() ) {
    		// remove first item from $blogs array, which should always be main blog
    		array_splice($blogs, 0, 1);
    	}
    	return $blogs;
    }
    add_filter( 'get_blogs_of_user', 'remove_main_blog_from_get_blogs' );

    Give it a try please and let me know if it worked!

    Best regards,
    Adam

  • Esther Goh Tok Mui

    Hi Adam,

    Thank you very much for your advise. I have one more question. I would like to list the sites that the user has registered, on the My-Account page. I copied the below codes from the wp-signup.php file and pasted it on the My-Account page and it provided a list of registered sites that belong to that user.

    <?php

    $blogs = get_blogs_of_user($current_user->ID);
    if ( !empty($blogs) ) { ?>

    <p><?php _e( 'You have registered for the following sites:' ) ?></p>

      <?php foreach ( $blogs as $blog ) {
      $home_url = get_home_url( $blog->userblog_id );
      echo '
    • ' . $home_url . '
    • ';

      } ?>

    <?php } ?>

    However, when the user has not registered for a site but only have an account, I want to change it to a message that says ' You have not registered for any site'. Can you advise me how to modify the above codes to do this?

    Many thanks for your help

    Best Regards
    Esther

  • Adam Czajczyk

    Hello Esther!

    Try this version of that code please:

    <?php
    
    $blogs = get_blogs_of_user($current_user->ID);
    if ( !empty($blogs) ) { ?>
    
    	<p><?php _e( 'You have registered for the following sites:' ) ?></p>
    
    	<?php
    		foreach ( $blogs as $blog ) {
    			$home_url = get_home_url( $blog->userblog_id );
    			echo '' . $home_url . '';
    		}
    }
    else { ?>
    
    	<p><?php _e( 'You have not registered for any site.' ) ?></p>
    <?php 
    
    }
    ?>

    Let me know if that works for you :slight_smile:

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.