Auto Multisite Registration

There are plugins for this, and I’m rather fond of them. If you just want all new users to be added to some (or all) sites, then I suggest you use Multisite User Management. It’s a great plugin, and lets you pick and chose. But similar to how sometimes you want users to register per site, you may have a situation where you want users to be added to any and all sites when they visit.

So how do we do this? It’s really not that painful, just make a add-users.php file in your mu-plugins folder with this:

<?php

/*
Plugin Name: Add Users
Description: Add users to blogs when they visit.
*/

function helf_add_users( ) {
    global $current_user, $blog_id;

    if(!is_user_logged_in())
    return false;
 
    if( !is_user_member_of_blog() ) {
        add_user_to_blog($blog_id, $current_user->ID, "subscriber");
    }
}

add_action( 'wp' , 'helf_add_users' , 10);

This code will automatically detect “John Doe is logged in.” and “John is not a member of this site.” If both of those are true, then it says “We will add him as a subscriber.” You can change subscriber as you see fit.

Would it be more efficient to just do this check on registration? Sure. But there are moments you don’t want this. If you wanted to get extra sneaky, you could set this to only run on a specific page, and make that page password protected.

If we want to add in a couple extra layers, that’s also not terrible. Let’s say we want people to press a button ‘Join This Site’? I have to admit, this actually took me longer to hash out than I wanted it to, mostly because I decided if I was going to do this, I should do it all the way.

That’s why I came up with Join My Multisite, which is a handy plugin to give you some more options. It’s per-site configurable, so your site-admins will get to decide if they want everyone logged in user to be added to their site, or if they want a widget, or if they want nothing at all. I put some time into thinking about if it would be a good idea to have the network admin be able to pick, but when I got down to brass tacks, I realized there was no way to easily force a widget (i.e. the button to join the site) for all sites and make it always look good. If you need that, you should fork the plugin and hard-code in the widget.

Join or Die

In a way, this is an extension of the old ‘Add Users Sidebar’ plugin, which drifted off into the wayward lands of not-supported. I never looked at that code, though, and instead wrote all this from scratch.

In addition to all that, Join My Multisite gives each site the ability, if registration is open, to easily make a per-site registration page. That one was Mason James’ suggestion.

StudioPress Theme of the Month

Comments

  1. Unbelievable. I spent hours solving this very same problem on a multisite install just yesterday. I ended up hacking ‘Multisite User Management’ (removing the conditional statements) so that it would perform what this piece of code does.

    Needless to say I’ll be migrating over to this immediately. Thank you!

    • Let me know if you find bugs/quirks or have any suggestions. I didn’t plan on writing it, but once I started sorting out the details, making the plugin was easier than detailing it out.

    • The only issue I’ve found with the code is the following line:

      add_user_to_blog($blog_id, $current_user->;ID, "subscriber");

      There is an extra semicolon ;) It worked find once I replaced it with:

      add_user_to_blog($blog_id, $current_user->ID, ‘subscriber’);

      Of course, I’ve re-purposed this to set the bbp_participant role so that users can post on all forums in a bbPress multisite install.

    • Bleah! Got it right in the plugin!

Trackbacks

Half-Elf? Try Half OFF WordPress ebooks!