Skip to content

Instantly share code, notes, and snippets.

@UVLabs
Created May 23, 2020 23:59
Show Gist options
  • Save UVLabs/44eccee252a37e5a4f2e04cb161554c1 to your computer and use it in GitHub Desktop.
Save UVLabs/44eccee252a37e5a4f2e04cb161554c1 to your computer and use it in GitHub Desktop.
WordPress - Import Users From CSV and Send Email
<?php
/*
Plugin Name: Import Users From CSV
Description: Imports users from a CSV file
Version: 1.0.0
Author: Uriahs Victor
Author URI: https://uriahsvictor.com
License: GPL v2
*/
function tld_import_users()
{
$plugin_dir = plugin_dir_path(__FILE__);
// Return example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/
$file = $plugin_dir . 'user_import.csv'; // Be sure to change this to the correct name of your csv file.
// Open the file.
$file_handle = fopen($file, "r");
if (empty($file_handle))
{
echo 'File not found. Double check file name and path.';
}
// Here we're simply specifying a page that should be visited for the importing code to run.
// You will need to change this id (55), to the page ID you want, or delete this block entirely...if you do code below will run on every page(which you don't want for this one time operation).
if (!is_page(55))
{
return;
}
$row_num = 0;
// Read first row but do nothing because we don't need column headers.
// NOTE: If you have no column headers remove this line or else you will be missing the first row of data.
fgets($file_handle);
echo "<pre>";
// Loop through the remaining CSV rows.
while (($row = fgetcsv($file_handle, 0, ",")) !== false)
{
// What do you want to do with the row number?
$row_num++;
// These indexes are unique to the csv file example.
// Here we're assuming the csv has the following columns(in that exact order): Name, Email, Phone, Address
// Use print_r($row); to see how your csv file is structured
// Change as necessary so you pull correct data.
$name = $row[0];
$email = $row[1];
$phone = $row[2];
$address = $row[3];
$username = strtolower(str_replace(" ", "", $name));
$username = str_replace("-", "", $username);
// We're just going to display some info on the page so you can see what's going on
echo 'Record #: ' . $row_num . '<br/><br/>';
echo 'Username: ' . $username . '<br/><br/>';
echo 'Email: ' . $email . '<br/><br/>';
echo 'Phone: ' . $phone . '<br/><br/>';
echo 'Address: ' . $address . '<br/><br/>';
echo '</br></br>';
// Create a password for the new user. See wp_generate_password() docs for more complex password creation.
// See: https://developer.wordpress.org/reference/functions/wp_generate_password/
$password = wp_generate_password(8);
$userdata = array(
'user_pass' => $password, //(string) The plain-text user password.
'user_login' => $username, //(string) The user's login username.
'user_nicename' => $name, //(string) The URL-friendly user name.
'user_email' => $email, //(string) The user email address.
'display_name' => $name, //(string) The user's display name. Default is the user's username.
'nickname' => $name, //(string) The user's nickname. Default is the user's username.
// Add other user data as needed.
// See: https://developer.wordpress.org/reference/functions/wp_insert_user/
);
// You can use newly created user's ID to maybe add some custom user meta
$new_user_id = wp_insert_user($userdata);
// Check if user already created
if (is_wp_error($new_user_id))
{
$error_code = array_key_first($new_user_id->errors);
$error_message = $new_user_id->errors[$error_code][0];
echo "Error occured: " . $error_message;
continue;
}
$array = array(
'username' => $username,
'password' => $password,
'email' => $email,
);
// User email. Change as needed.
$email_body = 'Your new account has been created! </br></br> Username: ' . $username . '</br></br> Password (change after login): ' . $password;
// Lets make this an HTML email since we're using <br> in email body
$headers = array(
'Content-Type: text/html; charset=UTF-8'
);
// Send email
wp_mail($email, 'Your Account Has Been Created!', $email_body, $headers);
echo '<hr>';
}
echo "</pre>";
// Close the file
fclose($file_handle);
}
// Using "wp" hook to run function. Change as necessary.
add_action('wp', 'tld_import_users');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment