Last active
October 29, 2015 15:28
-
-
Save jchudzynski/2bf36809013c29bfaae1 to your computer and use it in GitHub Desktop.
Bulk Import of Students
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Daniel | Ley | [email protected] | |
---|---|---|---|
Kyl | Cunn | [email protected] | |
Debah | Gler | [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('init', 'addStudents'); | |
function addStudents(){ | |
$file2 = dirname(__FILE__)."/students_data/students.csv"; | |
processCSVFile($file2,","); | |
} | |
function processCSVFile($filename='', $delimiter=',') | |
{ | |
if(!file_exists($filename) || !is_readable($filename)) | |
return FALSE; | |
$header = NULL; | |
$data = array(); | |
if (($handle = fopen($filename, 'r')) !== FALSE) | |
{ | |
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) | |
{ | |
$rowData = array(); | |
$rowData[]= $row[0] ;//name | |
$rowData[]= $row[1];//last name | |
$rowData[] =strtolower(substr(trim($row[0]),0,1).trim($row[1]));//user_id | |
$random_password = wp_generate_password( $length=8, $include_standard_special_chars=false ); | |
$rowData[] = $random_password;//password | |
$rowData[] = $row[2];//email | |
insertUser($rowData); | |
$data[]= $rowData; | |
} | |
fclose($handle); | |
} | |
return $data; | |
} | |
function insertUser($row){ | |
$firstname = $row[0]; | |
$lastname = $row[1]; | |
$username = $row[2]; | |
$password = $row[3]; | |
$email = $row[4]; | |
echo "Password for ".$username ."is: ".$password; | |
$user_id = username_exists( $username ); | |
if(!$user_id){ | |
$createdId = wp_create_user( $username, $password,$email); | |
if($createdId){ | |
wp_update_user( array( 'ID' => $createdId, 'first_name' => $firstname, 'last_name' => $lastname, 'role'=>'contributor' ) ); | |
echo "User Updated"; | |
} | |
} | |
else{ | |
echo "<br> User Exists: ".$username; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment