admin_url( 'admin-ajax.php' ) ) ); } function addmultiuser_style() { wp_register_style($handle = 'amu_css_style', $src = plugins_url('amustyle.css', __FILE__), $deps = array(), $ver = '1.0.0', $media = 'all'); wp_enqueue_style('amu_css_style'); } //MAIN FUNCTION function add_multiple_users() { //test again for admin priviledges if (!current_user_can('manage_options') ) { wp_die( __('You do not have sufficient permissions to access this page.') ); } //globals for functions global $current_user, $wpdb; get_currentuserinfo(); $thisUserEmail = $current_user->user_email; $thisBlogName = get_bloginfo('name'); $thisBlogUrl = site_url(); $confirmationStack = ''; //begin wrap class echo '
'; echo '
'; //if submitted if ( isset($_POST['addnewusers'] ) ) { //GET GENERAL OPTION SETTINGS // 1. send users emails if ( isset($_POST['sendpswemails'] ) ) { $sendEmail = 'yes'; } else { $sendEmail = 'no'; } // 2. email to user... if ( isset($_POST['yesConfirm'] ) ) { $yesConfirm = 'yes'; } else { $yesConfirm = 'no'; } // 3. set all to this role $setAllRoles = $_POST['allToRole']; // 4. force fill email... if ( isset($_POST['forcefillemail'] ) ) { $forceEmail = 'yes'; } else { $forceEmail = 'no'; } // 5. validate filled email addresses... if ( isset($_POST['validatemail'] ) ) { $balidateEmail = 'yes'; } else { $validateEmail = 'no'; } //BEGIN MESSAGE DISPLAY //wp-style feedback message echo '
'; echo '

New User User Accounts Processed!

'; echo '
'; //information feedback echo '

New User Information Added

'; echo '

If you did not request this information emailed to you and you wish to save the information for future reference, please copy the New User Information section below IMMEDIATELY and save it for you records. It will not be available again if the Add New form is submitted again or you navigate away from this page.
'; echo 'If any of your users have not been added due to an error, please note these errors and use the Add Multiple Users form below to attempt these registrations again.

'; echo '
'; echo '

New User Information

'; //run through registration lines by email for ( $icounter = 1; $icounter <= 50; $icounter += 1 ) { //get username/password/email/additional vars $tempusername = trim($_POST['username'.$icounter]); $password = trim($_POST['password'.$icounter]); $email = trim($_POST['email'.$icounter]); $userRole = $_POST['roleSetter'.$icounter]; $firstname = $_POST['firstname'.$icounter]; $lastname = $_POST['lastname'.$icounter]; $website = $_POST['website'.$icounter]; $emailGen = 'valid'; //if username is not blank if ($tempusername != '') { //process username $username = sanitize_user( $tempusername ); //check if username exists if ( username_exists( $username ) ) { $fail_userexists = '

'.$icounter.'. Error: The user '.$username. ' already exists. Please try adding this user again with a different username.

'; $confirmationStack = $confirmationStack.$fail_userexists; echo $fail_userexists; //check if email exists } else if ( email_exists( $email ) ) { $fail_emailexists = '

'.$icounter.'. Error:: The email address entered <'.$email.'> for the user '.$username.' already exists. Please try adding this user again with a unique email.

'; $confirmationStack = $confirmationStack.$fail_emailexists; echo $fail_emailexists; } else { //process password if ( $password == '' ) { //generate random password if blank $password = wp_generate_password(); } //process email if ( $email == '' ) { if ( $forceEmail == 'no' ) { $fail_noemailadded = '

'.$icounter.'. Error:: No email address for the user '.$username.' was included. Please try adding this user again with a valid email address.

'; $email = ''; $confirmationStack = $confirmationStack.$fail_noemailadded; echo $fail_noemailadded; } else { //generate random email address $email = 'temp_'.$username.'@temp'.$username.'.fake'; $emailGen = 'generated'; } } else { //validate entered password if set if ( $validateEmail == 'yes' ) { if ( !is_email( $email ) ) { $fail_emailnotvalid = '

'.$icounter.'. Error:: New user '.$username.' was not added because the email address provided '.$email.' for this user was not valid. Please try again using a valid email address or disable email verification.

'; $email = ''; $confirmationStack = $confirmationStack.$fail_emailnotvalid; echo $fail_emailnotvalid; } } } //VERIFY ALL DATA EXISTS THEN PROCESS if ( ( $username != '' ) && ( $password != '' ) && ( $email != '' ) ) { //confirmation of addition of user $userSuccess = '

'.$icounter.'. Success! The user '.$username.' has been added. Email Address for this user is '.$email.'. Password for this user is '.$password.'

'; $confirmationStack = $confirmationStack.$userSuccess; echo $userSuccess; //create new wordpress user $newuser = wp_create_user( $username, $password, $email ); $wp_user_object = new WP_User($newuser); //id of new user $newuserID = $wp_user_object->ID; //set user first name if ($firstname != '') { update_user_meta( $newuserID, 'first_name', $firstname ); } //set user last name if ($lastname != '') { update_user_meta( $newuserID, 'last_name', $lastname ); } //set user web site if ($website != '') { wp_update_user( array ('ID' => $newuserID, 'user_url' => $website) ) ; } //set user role if ($setAllRoles == 'notset') { $wp_user_object->set_role($userRole); } else { $wp_user_object->set_role($setAllRoles); } unset($wp_user_object); //send password to new user? if (($sendEmail == 'yes') && ($emailGen == 'valid')) { //set up email $to = $email; $subject = 'Your New User Account on '.$thisBlogName; $message = '

You have been registered on the '.$thisBlogName.' website.

You may now log into the site at '.$thisBlogUrl.'

Your username is '.$username.' and your password is '.$password.'

Regards,
'.$thisBlogName.'

'; $headers = 'From: '.$thisUserEmail.' <'.$thisUserEmail.'>' . "\r\n"; //filter to create html email add_filter('wp_mail_content_type',create_function('', 'return "text/html"; ')); //send email wp_mail($to, $subject, $message, $headers); } } } } else { //no username entered in this line, skip and move on } }//end for loop //SEND CONFIRMATION EMAIL TO LOGGED IN USER if ($yesConfirm = 'yes') { //set up confirmation email $confirmTo = $thisUserEmail; $confirmSubject = 'New User Account Information for '.$thisBlogName; $confirmMessage = '

This email is to confirm new user accounts for your website generated using the Add Multiple Users plugin.

All errors have also been included for reference when re-entering failed registrations.

'.$confirmationStack.'

End of message.

'; $confirmHeaders = 'From: '.$thisUserEmail.' <'.$thisUserEmail.'>' . "\r\n"; //filter to create html email add_filter('wp_mail_content_type',create_function('', 'return "text/html"; ')); //send email wp_mail($confirmTo, $confirmSubject, $confirmMessage, $confirmHeaders); echo '

This information has been emailed to your WordPress administrator email <'.$thisUserEmail.'> as requested.

'; } //on-screen confirmation if ($sendEmail = 'yes') { echo '

All users have been sent their login information (except where random emails were force-created).

'; } //close new user information div echo '
'; //end of add multiple users function } //SHOW ADDING FORM echo '

Add Multiple Users

'; echo '

Use the form below to add up to 50 users at once to your WordPress site.
'; echo 'Important - Please read the plugin information at the bottom of the page before first use.

'; echo '
'; //general options echo '
'; echo '

General Options (will apply to all new users added below):

'; echo '
'; echo '
'; //send emails... echo '
'; echo ' '; echo ' '; echo '
'; //send confirmstion email echo '
'; echo ' '; echo ' '; echo '
'; //validate emails... echo '
'; echo ' '; echo ' '; echo '
'; //force fill emails... echo '
'; echo ' '; echo ' '; echo '
'; //set all users to this role echo '
'; echo ' '; echo ' '; echo '
'; echo '
'; echo '
'; echo '

Your New User Details:

'; echo '
'; echo '
'; for ( $counter = 1; $counter <= 50; $counter += 1) { if ($counter & 1) { echo '
'; } else { echo '
'; } echo '
'; echo ' '.$counter.'.'; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo '
'; echo '
'; echo '  '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; //echo '
Validation Result:
'; //echo '
n/a
'; echo '
'; echo '
'; } echo '
'; //add submit button echo '
'; echo ' '; echo ' '; echo '
'; echo ''; echo '
'; echo '

Information about the Add Multiple Users plugin'; echo '

Information on form fields and settings

Usernames:
All new users must be given a unique username. Rows without a username are automatically skipped during the multiple registration process. Usernames cannot be changed once set. Usernames are automatically sanitized to strip out unsafe characters, but are not strictly sanitized.

Password:
May be set for each user, or left blank to generate a random password for that user. Passwords are not validated for their strength nor validity (yet). For more information on password strength and security, please visit the Hardening Wordpress page.

Email:
An unique, valid email address for each user is required (or use Force Fill option if emails are not available, see below). Emails will be checked for uniqueness and, if selected, validity.

FirstName, LastName, Website:
These parameters per user are optional and will be left blank if not filled in. Users or Administrators may update this information using the regular Wordpress user profile settings later. These fields are not validated.

'; echo '

Information on General Option settings

Email each new user their username and password:
If selected, automatically sends an email to each new registered user with their password, email, the name and host address of the website. Users who have been added with a "forced" email address will not be emailed (obviously).

Send me a complete list of new user account details:
Highly recommended. When you submit the multiple registration form, the results of your registration will display on the screen. However, this information will not remain on the screen once you navigate away from the page or submit the form again. This option emails all this information to your registered WordPress user account email.

Validate entered email addresses:
This setting affects both the in-page validation and the on-submit validation. It uses WordPress "is_email" verification. If you have trouble entering email addresses that you believe are valid, disable this option.

Force Fill empty email addresses:
Highly NOT recommended. This setting ignores empty email address fields that would normally cause that new user\'s registration to fail by creating a fake email address such as "temp_username@temp_username.info". It is very much recommended that all new users have a valid email address, and this function should only be used in cases where you need to register new users that do not have active email accounts.

Ignore individual User Role settings and set all new users to this role:
Does exactly what it says... ignores the User Role option for each new user and sets them to the role you choose here.

'; echo '

More Information

'; echo '

Visit the plugin page at http://www.happynuclear.com/sandbox/amu/add-multiple-users-for-wordpress.php for more information on the Add Multiple Users plugin.'; echo '

'; echo '
'; echo '
'; } //a call to action add_action( 'admin_menu', 'amu_menu' ); add_action( 'admin_print_styles', 'addmultiuser_style' ); add_action( 'admin_print_scripts', 'on_screen_validation' ); add_action( 'wp_ajax_UserNameValidation', 'validateUserName' ); add_action( 'wp_ajax_EmailValidation', 'validateEmail' ); //validation functions function validateUserName() { $username = $_POST['thevars']; if ( username_exists($username) ) { echo 'exists'; } else { echo 'spare'; } exit; } function validateEmail() { $email = $_POST['thevars']; $shouldValidate = $_POST['isValidated']; //if email validation is active if ($shouldValidate == 'yes') { if ( !is_email($email) ) { echo 'emailinvalid'; exit; } } //then check if exists if ( email_exists($email) ) { echo 'exists'; } else { echo 'spare'; } exit; } ?>