Limit one blog per user

Hi

I am trying to limit each user to be able to signup for only one site in my Multisite website. However, I am encountering a very strange problem. Previously I added the following codes which I obtained from a forum here and it worked perfectly for more than 2 months with more than 50 test sites created. Here's the code I used:

function ds_one_blog_only($active_signup) {
// get the array of the current user's blogs
$blogs = get_blogs_of_user( get_current_user_id() );
// all users may be members of blog 1 so remove it from the count, could be a "Dashboard" blog as well
if ($blogs["1"]) unset($blogs["1"]);
//if the user still has blogs, disable signup else continue with existing active_signup rules at SiteAdmin->Options
$n = count($blogs);
if($n == 1) {
$active_signup = 'none';
echo '';
} elseif($n > 1) {
$active_signup = 'none';
echo '';
} else {
$active_signup = $active_signup;
}
return $active_signup; // return "all", "none", "blog" or "user"
}
add_filter('wpmu_active_signup', 'ds_one_blog_only');

Then one day while testing some new feature, I discovered that when I register for a sub-site, the sub site first view land on a 404 page. But when I refresh, the sub-site is ok. Initially, I thought it was because I set the New Blog Template 'Make default' feature because that was the last action I did just before I discovered that problem. So I undo the 'Make default' but the problem remained. Then I removed all codes in my functions.php and recreate new sites for new blog templates and do not set default in the New Blog template. Registration of new sites was resumed to normal and working well. However the moment I added the above codes ( the only codes added in function.php), the problem appear --- new sites will first land on a 404 page but will work well when click refresh.

I tried creating my website functionalities on a staging site. I copied over the same functions.php with the above codes to limit one blog per user, I copied the wp-signup.php I replicated all the plugins I have used on my production site and I even set 'Make default' in the New Blog Template, I cannot replicate the 404 problem on the staging site. Registration of new sites and limiting to one blog per user and all other functionalities worked perfectly well on the staging site. Very strange. The only difference between my staging site and my production site is that the production site is using https SSL secure hosting while the staging site does not have a SSL certificate. Could this be the cause? Yet the strange thing is the limit to one blog per user did work well on my production website for quite a long time.

So I removed the above codes and tried the following new codes:

function limit_blog_creation_per_user($active_signup)
{
$blog_limit = 1;
if( !is_super_admin() )
{
$current_user = wp_get_current_user();
$user_blogs = get_blogs_of_user( $current_user->ID );
if (count($user_blogs) >= $blog_limit ) $active_signup = 'none';
}
return $active_signup;
}
add_action('wpmu_active_signup','limit_blog_creation_per_user');

This time, I tested with the first site registration, it works perfectly!! No 404 page. However, when I test a a second site creation, and click on the new site URL to view, it took a long time but finally displayed the new site well. Then I tested a third time, this time, it landed on a 404 page again. Strange!

I hope someone in WPMUDEV team can shed some light to what could be the possible cause of the above problem.

Looking forward to hearing from you!

Esther

  • Luís
    • Support

    Hi Esther ,

    Hope you're doing well today!

    I am testing the code you mentioned above (the first code) in a site without SSL and I was not able to replicate too.

    Just to make sure, the 404 error is in the first blog created by the user, right? Not when you try to create another blog witht the same user?

    Could you follow the flowchart of the below page to determine if another plugin/theme may be conflicting with this, just to make sure?

    https://premium.wpmudev.org/manuals/getting-support/

    I flagged the SLS team (our code experts :slight_smile: ) to investigate further and get their valuable feedback.

    Since this team deals with more advanced threads it may take them longer to reply.

    Cheers, Luís

  • Esther Goh Tok Mui
    • Site Builder, Child of Zeus

    Hi Luis,

    Thank you for your prompt reply. Really appreciate you going the extra mile to activate the code experts. Thank you very much.

    Yes, the 404 error is in the first blog created by the user.

    Looking forward to hearing from your SLS team.

    Best Regards

    Esther

  • Jude
    • DEV MAN

    Hey Esther

    Can you try this code instead ?

    function limit_blog_creation_per_user($active_signup)
    {
    
    $blog_limit = 1;
    if( !is_super_admin() ) :
    
    $current_user = wp_get_current_user();
    $user_blogs = get_blogs_of_user( $current_user->ID );
    if (count($user_blogs) >= $blog_limit ) {
        $active_signup = 'none';
    }
    
    endif;
    return $active_signup;
    
    }
    add_action('wpmu_active_signup','limit_blog_creation_per_user');

    Cheers
    Jude

  • Esther Goh Tok Mui
    • Site Builder, Child of Zeus

    Hi Jude,

    Thank you very much for your help and providing me with the revised code.

    I have also done a plugin check base on the flow chart provided by Luis in https://premium.wpmudev.org/manuals/getting-support/

    I deactivated all plugins, removed all additional functions, changed the theme to default theme and then activate plugin one by one, introducing back the additional functions one at a time. I found the plugin that is causing this problem to be 'Automessage plugin'. When I remove this Automessage plugin the problem of 404 page disappeared. I tested creation of 21 new sites and the 404 page never appear again, even with the above first code.

    Thank you very much. Now I can successfully limit one blog per user and have all other features working as well. The only feature which I cannot have is the auto sending of emails to users who have created a site.

    Thank you very much for going the extra mile to help. Many, many thanks!

    Have a great day!

    Esther

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.