• I’m in the process of launching a Multisite Network and just spent the evening looking for solutions to limit the number of Multisite registrations to a finite amount. I’m not particularly happy with the WordPress default which allows a single admin to create an unlimited number of sites.

    Can someone please provide assistance?

    Thanks,
    Don

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Don

    (@55don)

    I see my OP cannot be edited any longer; however, in an effort to clarify my goal, I’m hoping someone can help me implement the following logic.

    If $_SERVER[‘PHP_SELF’] shows the admin is on /wp-signup.php and the admin is not the Network Administrator and count(get_blogs_of_user($current_user->ID)) > n sites, echo a message requesting the admin remove a site and try again… or something to that effect.

    There’s another issue though. If the subsite admin currently owns <= n sites, the default WP message on wp-signup.php will display, “…There is no limit to the number of sites you can have…”. This message is printed to the browser by the function signup_another_blog() which starts at line 275 of wp-signup.php and I don’t know how to modify it (or replace it) without hacking the core.

    Thank you very much,
    Don

    • This reply was modified 7 years, 5 months ago by Don.

    I think I deleted my post somehow, so I will post it again.

    Put the code in a plugin.

    This code changes the message, hides the form and makes it to not work for users with at least 5 sites that are not super admin.

    Let me know if you have questions. You will need to test it and improve it more, since I did it quickly and not perfectly.

    if ( 'wp-signup.php' === $GLOBALS['pagenow'] || '/wp-signup.php' === $_SERVER['PHP_SELF'] ) {
        add_filter( 'gettext', 'my_change_blog_signup_function', 20, 3 );
        add_action( 'wp_head', 'my_custom_css_for_wp_signup' );
        add_filter( 'wpmu_validate_blog_signup', 'my_validation_for_wp_signup' );
    }
    
    function my_change_blog_signup_function( $translated_text, $untranslated_text, $domain ) {
        if ( $untranslated_text == 'Welcome back, %s. By filling out the form below, you can <strong>add another site to your account</strong>. There is no limit to the number of sites you can have, so create to your heart’s content, but write responsibly!' && ! current_user_can( 'manage_network' ) ) {
            $translated_text = 'Welcome back, %s. By filling out the form below, you can <strong>add another site to your account</strong>. You can have up to 5 sites.';
            $user_blogs = get_blogs_of_user( get_current_user_id() );
            if ( count( $user_blogs ) >= 5 ) {
                $translated_text .= '<br> <b>You cannot add more sites!</b>';
            }
        }
        return $translated_text;
    }
    
    function my_custom_css_for_wp_signup() {
        if ( is_user_logged_in() ) {
            $user_blogs = get_blogs_of_user( get_current_user_id() );
            if ( count( $user_blogs ) >= 5 && ! current_user_can( 'manage_network' ) ) {
                 echo '<style> #setupform { display: none; } </style>';
            }
        }
    }
    
    function my_validation_for_wp_signup( $content ) {
        if ( is_user_logged_in() ) {
            $user_blogs = get_blogs_of_user( get_current_user_id() );
            if ( count( $user_blogs ) >= 5 && ! current_user_can( 'manage_network' ) ) {
                $content['errors']->add( 'my_awesome_error', 'limit to 5 sites' );
            }
        }
        return $content;
    }
    Thread Starter Don

    (@55don)

    Thank you for the code, nnikolov… and thank you for the quick reply, I was not expecting that! :^)

    Your solution is light years ahead of the default WordPress action, unfortunately my_change_blog_signup_function() is not working. I’ll need to do some research because this is my first attempt at WordPress development… very interesting stuff!

    For now, I have your code inserted into the functions.php file of my current theme since it is the theme dedicated to my Multisite. Perhaps when I pick up more knowledge, I’ll utilize this code as a plugin.

    Thanks again,
    Don

    Thread Starter Don

    (@55don)

    Nikolay,

    The only part of your code that modifies wp-signup.php from my theme’s functions.php is my_custom_css_for_wp_signup() which faithfully writes:

    <style> #setupform { display: none; } </style>

    to the <head> of wp-signup.php.

    Is there a way to check current_user_can(‘manage_network’) in the theme’s functions.php file; and, if it returns false, continue with a substitution for wp-signup.php stored in functions.php? Is it possible to trigger an entire substitution for a core file? As an administrator of a Multisite, it doesn’t bother me to have the capability to create sites until my disk is full.

    Thanks,
    Don

    • This reply was modified 7 years, 4 months ago by Don.
    • This reply was modified 7 years, 4 months ago by Don.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Limit the Number of Multisite Registrations’ is closed to new replies.