DDImport Users'. Author: Dagon Design Version: Author URI: http://www.dagondesign.com/ Contributor: Nicholas LaRacuente, http://www.sccs.swarthmore.edu/users/10/ndl */ $ddui_version = '1.1'; 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)email
   username(delimiter)email
   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_e) = @split($delimiter, $ut, 2))){ $result .= "

Regex ".$delimiter." not valid.

"; } $u_n = trim($u_n); $u_e = trim($u_e); if (($u_n != '') && ($u_e != '')) { $u_data[$i]['username'] = $u_n; $u_data[$i]['email'] = $u_e; $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['email']) . ''; 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 password $password = substr(md5(uniqid(microtime())), 0, 7); // create user $user_id = wp_create_user($ud['username'], $password, $ud['email']); if (!$user_id) { $errors[] = 'System error! Could not add: ' . $user_line; } else { wp_new_user_notification($user_id, $password); $complete++; // set role $ruser = new WP_User($user_id); $ruser->set_role($the_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 automatically generated 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 and email(default "[|]").
May type username|email pairs directly.

May submit a file.

Role for these users: