Home » Import Users From CSV In WordPress Programmatically

Import Users From CSV In WordPress Programmatically

Last updated on July 3, 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.


Sr. No.	First Name	Surname 	Username	Email	             Website	             Password
1	    John	Doe	        john.doe77	[email protected]  http://www.johndoe.com  [email protected]
2	    ABC	        ABC	        abc.abc77	[email protected]	     http://www.abc.com	     [email protected]
3	    XYZ	        XYZ	        xyz.xyz77	[email protected]	     http://www.xyz.com	     [email protected]
4	    DEF	        DEF	        def.def77	[email protected]	     http://www.def.com	     [email protected]
5	    GHI	        GHI	        ghi.ghi77	[email protected]	     http://www.ghi.com	     [email protected]
6	    JKL	        JKL	        jkl.jkl77	[email protected]	     http://www.jkl.com	     [email protected]
7	    MNO	        MNO	        mno.mno77	[email protected]	     http://www.mno.com	     [email protected]
8	    PQR	        PQR	        pqr.pqr77	[email protected]	     http://www.pqr.com	     [email protected]
9	    STU	        STU	        stu.stu77	[email protected]	     http://www.stu.com	     [email protected]
10	    VWX	        VWX	        vwx.vwx77	[email protected]	     http://www.vwx.com	     [email protected]

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'




		  	// Close the file

	add_action('init', 'sc_import_users_from_csv');


Additionally, read our guide on After Login Redirect To Previous Page 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!


Leave a Comment