My given task for the day is to figure out how to write a custom script that will create a new user and assign that user to a new website on a WordPress MultiSite installation. There are many plugins out there that do this for you but most of them, like gravityforms, unfortunately integrate with one payment processor; paypal. We need ours to integrate with a much more complex system that uses authorize.net to process credit cards and it needs the ability to take Promo Codes. I cant find anything out there that does this so ive resorted to building it from scratch.
Heres what ive learned so far. There are four wordpress functions that are key in making a smooth site registration work. First you must make use of the new site validation functions which are:
$username_exists = username_exists($user_name); // check sanitized username
$email_exists = email_exists($user_email);
if ($username_exists) die("username already $user_name in use.");
if ($email_exists) die("the email $user_email is already in use. ");
// get validation result for multi-site fields
$validation_result = wpmu_validate_blog_signup($site_address, $site_title);
$error_msg = (isset($validation_result['errors']->errors['blogname'][0])) ? $validation_result['errors']->errors['blogname'][0] : false;
if($error_msg != false) die($error_msg);
$error_msg = (isset($validation_result['errors']->errors['blog_title'][0])) ? $validation_result['errors']->errors['blog_title'][0] : false;
if($error_msg != false) die($error_msg);
Assuming your form has passed the necessary fields $user_name, $user_email, $site_address and $site_title. These functions will make sure the username or the site address doesn’t already exists. if it does it simply halts the script.. Obviously you may want to create a more graceful error notice to the user.
Once all the important information is validated, you can move on to create the username:
</span>
<pre>$user_id = wp_create_user( $user_name, $password, $user_email );
if(is_wp_error($user_id)) die($user_id->get_error_message());
// set user metadata
update_user_meta($user_id, 'first_name', $firstname);
update_user_meta($user_id, 'last_name', $lastname);
update_user_meta($user_id, 'phone', $phone);
update_user_meta($user_id, 'package', $package);
// set user role
if(!empty($role) || $role == 'subscriber') {
$userobj = new WP_User($user_id);
$userobj->set_role($role);
}
// send user/admin notifications
wp_new_user_notification($user_id, $password);
And finally create the site and assign the user we just created as administrator of that site:
create_new_multisite($user_id);
function create_new_multisite($user_id) {
global $wpdb, $current_site, $base, $site_address, $site_title;
$user = get_userdata($user_id);
// get the domain name
$domain = '';
if(!preg_match( '/(--)/', $site_address) && preg_match('|^([a-zA-Z0-9-])+$|', $site_address) )
$domain = strtolower($site_address);
// get the site title and user email
$email = $user->user_email;
// final check to make sure our essentials are good to go
if(empty($domain) || empty($email) || !is_email($email))
die('no domain or email or invalid email');
if(is_subdomain_install()){
$newdomain = $domain . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
$path = $base;
} else {
$newdomain = $current_site->domain;
$path = $base . $domain . '/';
}
// create the new site!
$id = wpmu_create_blog($newdomain, $path, $site_title, $user_id , array( 'public' => 1 ), $current_site->id);
if(is_wp_error($id)) { die($id->get_error_message()); }
$dashboard_blog = get_dashboard_blog();
if(!is_super_admin($user_id) && get_user_option('primary_blog', $user_id) == $dashboard_blog->blog_id)
update_user_option( $user_id, 'primary_blog', $id, true );
//send new site notifications
$content_mail = sprintf(__( "New site created by %1s\n\nAddress: http://%2s\nName: %3s", "jummelreg"), $current_user->user_login , $newdomain . $path, stripslashes($site_title));
wp_mail(get_site_option('admin_email'), sprintf(__('[%s] New Site Created', "jummelreg"), $current_site->site_name ), $content_mail, 'From: "Site Admin" wpmu_welcome_notification($id, $user_id, $password, $site_title, array('public' => 1));
}
