DDImport Users'. Author: Dagon Design Version: 1.1.13 Author URI: http://www.dagondesign.com/ Contributor: Nicholas LaRacuente, http://www.sccs.swarthmore.edu/users/10/ndl */ /* 2009-06-17 - Modified by Robert McKenzie (rmckenzi@rpmdp.com) http://www.gammaray-tech.com Modifications to this script include the ability to supply a personal URL when creating accounts 2009-01-24 - Modified by Robert McKenzie (rmckenzi@rpmdp.com) http://www.gammaray-tech.com Modifications to this script include the ability to specify a password as well as first and last names. Firstname, Lastname and Password are optional but the fields must be delimited, ie: bill|Bill|Smith|blahblah|bill.smith@blah.com|http://blah.site.com jim|Jim|||Jim@blah.com In the case of a missing password the script will generate one as the original script did and email that password to the new user. If the password has been specified it will also be sent to the user */ $ddui_version = '1.1.13'; function ddiu_add_management_pages() { if (function_exists('add_management_page')) { add_management_page('Import Users', 'DDImportUsers', 8, __FILE__, 'ddiu_management_page'); } } #can specify how to parse submitted file by editing this function function fileParseFunction($filename){ return file($filename); } #modify this function to specify how to parse text in field #could change format or add validation function fieldParseFunction($text){ return explode("\n", trim($text)); } #specify format information to be displayed to the user $formatinfo = '

The data you enter MUST be in the following format:
   username(delimiter)firstname(delimiter)lastname(delimiter)password(delimiter)email(delimiter)role(delimiter)UserURL
   username(delimiter)firstname(delimiter)lastname(delimiter)password(delimiter)email(delimiter)role(delimiter)UserURL
   etc...

'; function ddiu_management_page() { global $wpdb, $wp_roles, $formatinfo, $ddui_version; $result = ""; if (isset($_POST['info_update'])) { ?>

No names entered in field.

"; } if ($_FILES['ddui_file']['error'] != UPLOAD_ERR_NO_FILE){#Earlier versions of PHP may use $HTTP_POST_FILES $file = $_FILES['ddui_file']; if($file['error']){ $result .= '

Errors!

'; switch ($file['error']){ case UPLOAD_ERR_INI_SIZE: $result .= "File of ".$file['size']."exceeds max size ".upload_max_filesize; break; case UPLOAD_ERR_FORM_SIZE: $result .= "File of ".$file['size']."exceeds max size ".upload_max_filesize; break; case UPLOAD_ERR_PARTIAL: $result .= "File not fully uploaded"; break; default: } $result.='.

'; } elseif(!is_uploaded_file($file['tmp_name'])){ $result = "File ".$file['name']." was not uploaded via the form."; } else{ #should be ok to read the file now $u_temp = array_merge($u_temp, fileParseFunction($file['tmp_name'])); } } else{ $result .= "

No file submitted.

"; } $u_data = array(); $i = 0; foreach ($u_temp as $ut) { if (trim($ut) != '') { if (! (list($u_n, $u_f, $u_l, $u_p, $u_e, $u_r, $u_u) = @split($delimiter, $ut, 7))){ $result .= "

Regex ".$delimiter." not valid.

"; } $u_n = trim($u_n); $u_f = trim($u_f); $u_l = trim($u_l); $u_p = trim($u_p); $u_e = trim($u_e); $u_r = trim($u_r); $u_u = trim($u_u); //if (($u_n != '') && ($u_f != '') && ($u_l != '') && ($u_p != '') && ($u_e != '')) { if (($u_n != '') && ($u_e != '')) { $u_data[$i]['username'] = $u_n; $u_data[$i]['firstname'] = $u_f; $u_data[$i]['lastname'] = $u_l; $u_data[$i]['password'] = $u_p; $u_data[$i]['email'] = $u_e; $u_data[$i]['role'] = $u_r; $u_data[$i]['url'] = $u_u; $i++; } } } // print_r($u_data); // process each user $errors = array(); $complete = 0; foreach ($u_data as $ud) { // check for errors $u_errors = 0; $user_line = '' . htmlspecialchars($ud['username']) . '|' . htmlspecialchars($ud['firstname']) . '|' . htmlspecialchars($ud['lastname']) . '|' . htmlspecialchars($ud['password']) . '|' . htmlspecialchars($ud['email']) . '|' . htmlspecialchars($ud['role']) . '|' . htmlspecialchars($ud['url']) . ''; if (!is_email($ud['email'])) { $errors[] = 'Invalid email address: ' . $user_line; $u_errors++; } if (!validate_username($ud['username'])) { $errors[] = 'Invalid username: ' . $user_line; $u_errors++; } if (username_exists($ud['username'])) { $errors[] = 'Username already exists: ' . $user_line; $u_errors++; } $email_exists = $wpdb->get_row("SELECT user_email FROM $wpdb->users WHERE user_email = '" . $ud['email'] . "'"); if ($email_exists) { $errors[] = 'Email address already in use: ' . $user_line; $u_errors++; } if ($u_errors == 0) { // generate passwords if none were provided in the import if ($u_p == '') { $password = substr(md5(uniqid(microtime())), 0, 7); } else { $password = $ud['password']; } // create user $user_id = wp_insert_user(array( "user_login" => $ud['username'], "first_name" => $ud['firstname'], "last_name" => $ud['lastname'], "user_pass" => $password, "user_email" => $ud['email'], "user_url" => $ud['url']) ); if (!$user_id) { $errors[] = 'System error! Could not add: ' . $user_line; } else { wp_new_user_notification($user_id, $password); $complete++; // set role if ($ud['role'] == '') { $ruser = new WP_User($user_id); $ruser->set_role($the_role); } else { $ud['role'] = strtolower($ud['role']); $ruser = new WP_User($user_id); $ruser->set_role($ud['role']); } } } } // show result if ($complete > 0) { $result .= "

Processing complete: " . $complete . " users imported!

"; $result .= "

Role: " . $the_role . "

"; $result .= "

(These users will receive an email notification with their assigned password.)

"; } if ($errors) { $result .= '

Errors!

'; } // // END Processing // } ?>

Import Users v

For information and updates, please visit:
http://www.dagondesign.com/articles/import-users-plugin-for-wordpress/

'; echo '

Results

'; echo trim($result); echo '
'; } ?>
" >

User Data

May specify the (regex) delimiter between name, email and password (default "[|]").
May type username|firstname|lastname|password|email|role|url pairs directly.

May submit a file.

Role for these users: