Multisite Per Site Registration

This post is dedicated to TJ List, who donated to help me get to WCSF. He’s the last post of this ilk, so I’ll stop having these announcements, but I wanted to say thank you all, one more time. My new job starts the 27th, and you all helped!

Edit: If you want a plugin, see Join My Multisite

Sometimes people want a way to sign up for the network on specific sites, and not go back to the main site. And I often tell people ‘You need to make a custom page template with the code from the signup page in it.’

But how? There’s going to be a lot of code in this post, so at the end I’ll give you a link to the whole file you can download and play with. We’re going to go through the steps, however, since we may not be using the same theme, and you’ll need to understand the process.

Make your Page Template

First you make a new page template. I always call mine page-template-signup.php because well named files/code don’t need as much inline documentation.

Since I wanted to build this off of TwentyTwelve, I grabbed the full page template, and pulled that over:

<?php
/*
Template Name: Signup
*/

get_header(); ?>

        <div id="primary" class="site-content">
                <div id="content" role="main">

                        My Content Here

                </div><!-- #content -->
        </div><!-- #primary .site-content -->

<?php get_footer(); ?>

Obviously on a first pass, nothing much is going on here. The next step is to open up wp-signup.php and take a look. There’s a lot going on here, but we don’t need all of it.

First let’s look at what we need to add to the header area. Most of this is just styling and a little tweakage to stop search engines from thinking people need to really find this page, so let’s add the following right above get_header();

add_action( 'wp_head', 'wp_no_robots' );

// Fix for page title
$wp_query->is_404 = false;

function wpmu_signup_stylesheet() {
	?>
	<style type="text/css">
		.mu_register { width: 90%; margin:0 auto; }
		.mu_register form { margin-top: 2em; }
		.mu_register .error { font-weight:700; padding:10px; color:#333333; background:#FFEBE8; border:1px solid #CC0000; }
		.mu_register input[type="submit"],
			.mu_register #blog_title,
			.mu_register #user_email,
			.mu_register #blogname,
			.mu_register #user_name { width:100%; font-size: 24px; margin:5px 0; }
		.mu_register .prefix_address,
			.mu_register .suffix_address {font-size: 18px;display:inline; }
		.mu_register label { font-weight:700; font-size:15px; display:block; margin:10px 0; }
		.mu_register label.checkbox { display:inline; }
		.mu_register .mu_alert { font-weight:700; padding:10px; color:#333333; background:#ffffe0; border:1px solid #e6db55; }
	</style>
	<?php
}

add_action( 'wp_head', 'wpmu_signup_stylesheet' );

Just below get_header(); we’re going to add do_action( 'before_signup_form' );

Then in the ‘My Content Here’ section, you’re going to copy everything from <div id="content" class="widecolumn"> down to <?php do_action( 'after_signup_form' ); ?> and put it in there. It’s a lot of code, I know. I’m not including it here because of that. Again, check the attached file.

Finally search your file for all instances of wp-signup.php and change them. To what? To whatever you’re going to name your page in the next section. I knew I wanted to name my page signup, so I replaced it with that.

Make Your Page

Picking Page TemplateNext you make a page. This is easy. You go in to your site, you make a page (I called mine Signup) and you put in no content. Pick the page template called ‘signup’ and save. I always opt to turn off comments and pingbacks on this page for what I hope are obvious reasons. Don’t bother putting in any content to the post. Because we didn’t leave in the loop, it wouldn’t display.

If that’s something you want, then you would leave the loop info in where I had ‘My Content Here’


                        <?php while ( have_posts() ) : the_post(); ?>
                                <?php get_template_part( 'content', 'page' ); ?>
                                <?php comments_template( '', true ); ?>
                        <?php endwhile; // end of the loop. ?>

That’s an ‘as you like it’ sort of thing. If you need to leave messages, etc, it’s great.

And that’s kind of it. Oh, you’ll want to mess with your CSS, but that’s all you need to get started! It even keeps you on the page.

How it looks…

Since there are multiple registration options, here’s how they look:

With Registration Disabled

User accounts may be registered

Logged in users may register new sites

Only Logged In Users Can Make Sites

Both sites and user accounts can be registered

Adding Users to Sites

I know, I said we were done. There’s one more thing. What happens if you want that per site registration to automatically add people to the site they’re on? That is, if someone goes to halfelf.org/signup/ I want them to be added to halfelf.org as a user. The problem with this is when you register for a user account, you may have noticed there’s an ‘extra’ step to confirm. This is where you get that email that says ‘click here…’

The ‘good’ thing is the URL you get looks like this http://halfelf.org/wp-activate.php?key=0ef92965187a50be and it will take you back to the right site to login! The ‘bad’ thing is it doesn’t actually add you to the site. It will add you any time you create a site, but if you just make a login ID, you get this:

No Access Is Bad

As you can see from that URL, it’s sending me back to wp-activate.php, which is something we can’t change in our signup template. If you know you want to add all users to all sites, then you want Multisite User Management, but that isn’t always the case. So what do we do? Option one is to grab a plugin that makes us approve all users, then we can add them as they’re requested. But you don’t want to do manual work, I’d guess.

The trick is to add an action onto user registration, but only run it per site. So you’d want this in an mu-plugin, and since halfelf happens to be blog #2, I do this:

<?php
global $blog_id;
if ( $blog_id == 2 ) {

  function helf_activate_user( $user_id, $password, $meta )
  {add_user_to_blog( '2', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );}
  add_action( 'wpmu_activate_user', 'helf_activate_user', 10, 3 );
}
?>

That says ‘Hey, if I’m on blog ID 2, when people register, kick them to be a subscriber on this blog. I have mine in a plugin called halfelf-autoregistration.php and it works rather nicely. If someone registers on ipstenu.org, even if they try to login to halfelf, they can’t login because their registration process was on the other site. 1

Do you have any tricks that help you do this?

Oh and here’s the file to download. It’s in .txt for security reasons.

Download (TXT)

Notes:

  1. I suppose if someone was clever, they could take http://halfelf.org/wp-activate.php?key=0ef92965187a50be and change it to http://ipstenu.org/wp-activate.php?key=0ef92965187a50be to register there, but there’s no change in ‘risk’ between being a network psudeo subscriber and a real subscriber. If I was adding in editors, I’d probably change my mind here and manually validate every new user. I’m trusting in the fact that most people aren’t that clever.
StudioPress Theme of the Month
Half-Elf? Try Half OFF WordPress ebooks!