Home » Import Users From CSV In WordPress Programmatically

Import Users From CSV In WordPress Programmatically

Last updated on December 23, 2020 by

After creating a new site, your client might want to import users from the old non WordPress site. So in such a case, they can give you a CSV (Comma Separated Values) or Excel file to import users to WordPress. In this tutorial, we will see how to import users from CSV in WordPress. Let’s just jump into it.

An Ideal Place to Add Code in WordPress

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please also note that we have tested all codes in the GeneratePress child theme & Storefront child theme.

Importing Users From CSV To WordPress

  1. So here is our users.csv file with minimal columns, your CSV might have fewer or more columns. You can customize the user import code as per your requirement.

    Put your CSV file into your current activated theme directory and change its name to users.csv or change the filename on line no. 8 in the following user import code.

users.csv

Sr. No.	First Name	Surname 	Username	Email	             Website	             Password
1	    John	Doe	        john.doe77	johndoe@example.com  http://www.johndoe.com  Johndoe@12345
2	    ABC	        ABC	        abc.abc77	abc@example.com	     http://www.abc.com	     Abc@12345
3	    XYZ	        XYZ	        xyz.xyz77	xyz@example.com	     http://www.xyz.com	     Xyz@12345
4	    DEF	        DEF	        def.def77	def@example.com	     http://www.def.com	     Def@123456
5	    GHI	        GHI	        ghi.ghi77	ghi@example.com	     http://www.ghi.com	     Ghi@123457
6	    JKL	        JKL	        jkl.jkl77	jkl@example.com	     http://www.jkl.com	     Jkl@123458
7	    MNO	        MNO	        mno.mno77	mno@example.com	     http://www.mno.com	     Mno@123459
8	    PQR	        PQR	        pqr.pqr77	pqr@example.com	     http://www.pqr.com	     Pqr@123460
9	    STU	        STU	        stu.stu77	stu@example.com	     http://www.stu.com	     Stu@123461
10	    VWX	        VWX	        vwx.vwx77	vwx@example.com	     http://www.vwx.com	     Vwx@123461

Notes: After importing the users from CSV to WordPress remove or comment out the code otherwise, code will execute on each page refresh. It will not create duplicate users but it will impact on site performance.

  1. Now, add the following code into your theme’s function.php file. After adding the following code, you just need to refresh the page and all the users will be imported from the CSV to WordPress within a few minutes. Import might take time only if you have large number of users.
if ( !function_exists('sc_import_users_from_csv') ) :

	function sc_import_users_from_csv() {

		/*@ prevent sending new registration emails */
		remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
		
		$file = get_stylesheet_directory_uri().'/users.csv';

		// Open the file for reading
		if (($h = fopen("{$file}", "r")) !== FALSE) :
		
		    // Each line in the file is converted into an individual array that we call $data
		    // The items of the array are comma separated
		  	while (($data = fgetcsv($h, 1000, ",")) !== FALSE) :
		                // Prevent duplication if username is already exists
				if ( !username_exists($data[3]) ) :

			    	$new_user = array(
				        'user_pass'  => $data[6],
				        'user_login' => $data[3],
				        'user_email' => sanitize_email($data[4]),
				        'first_name' => sanitize_text_field($data[1]),
				        'last_name'  => sanitize_text_field($data[2]),
				        'user_url'   => esc_url_raw($data[5]),
				        'role'       => 'subscriber'
				    );

				    wp_insert_user($new_user);

				endif;

		  	endwhile;

		  	// Close the file
		  	fclose($h);

		endif;
	}
	add_action('init', 'sc_import_users_from_csv');

endif;

Additionally, read our guide:

  1. How to Add Products Per Page Dropdown in WooCommerce
  2. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  3. How to Create a Plugin in WordPress from Scratch
  4. How to Disable Admin Bar in WordPress Without Plugin
  5. How To Send Custom Emails in WordPress
  6. How to Allow Preview of Draft Post Without Login in WordPress
  7. How To Logout Without Confirmation In WordPress

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!

 
 

6 thoughts on “Import Users From CSV In WordPress Programmatically”

  1. Hi there, I would like to subscribe for this weblog to take most recent updates, therefore where can i do it please assist. Tonie Napoleon Hoye

    Reply
  2. Hi, yup this paragraph is genuinely pleasant and I have learned lot of things from it on the topic of blogging. thanks. Almeria Tam Lil

    Reply

Leave a Comment

*

code