I need to import users to my WP database once a day. So I have a script that runs once a day to do an action. The problem that I'm having is that I'm a little over my head on this one.

Another company provides me with userdata in a .csv file. I then need to import this file to my WP database.

The script I use for running once a day is the following (this is in my functions.php):

add_action( 'wp', 'prefix_setup_schedule' );
* On an early action hook, check if the hook is scheduled - if not, schedule it.
function prefix_setup_schedule() {
    if ( ! wp_next_scheduled( 'prefix_hourly_event' ) ) {
        wp_schedule_event( time(), 'hourly', 'prefix_hourly_event');

add_action( 'prefix_hourly_event', 'prefix_do_this_hourly' );
* On the scheduled action hook, run a function.
function prefix_do_this_hourly() {
    wp_mail( '[email protected]', 'Automatic email sent at '.time(), 'Automatic scheduled email from WordPress.');

On the server runs a cronjob that activates wp-cron.php once a day. If there aren't any users the script won't run so I activate it. Once there is activity it runs.

This is a simple working example of an email that is sent every hour. Now I need to import users.

Here is what I found:

LOAD DATA INFILE 'path/to/file/users.csv'
INTO TABLE discounts

I don't know if this works I'm not there yet. The problem I'm facing now it that the usersystem of WordPres exists of two tables wp_user and wp_usermeta.

Can anybody tell me if I'm even on the right track and how to solve this. Is there maybe a way to seperate the .csv file and select what to import where including the fact that there is a refference between the user and usermeta tables?


I changed my way of thinking and want to do the following:

$row = 1;
if (($handle = fopen(get_bloginfo('template_directory')."/import_test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";

This is a direct copy from http://php.net/fgetcsv.
Now I want to replace the for loop with an wp_create_user function to add the user.
At the same time I need to add user_meta to that user (I think). Any ideas how I can add a user with metafields and select the right values from my csv file matching the vars?

  • EDIT -
    I have come so far but I can't get the last part to work. I have the adding of a new user fixed.
    The foreach loops through the csv to add all users:

    $filepath = get_bloginfo('template_directory')."/import_test.csv";
    $file = fopen($filepath, "r") or die("Error opening file");
    $i = 0;
    while(($line = fgetcsv($file, 1000, ";")) !== FALSE) {
        if($i == 0) {
        $c = 0;
        foreach($line as $col) {
            $cols[$c] = $col;
    } else if($i > 0) {
    $c = 0;
        foreach($line as $col) {
            $data[$i][$cols[$c]] = $col;


    foreach ($data as $gebruiker){
        $username = $gebruiker['username'];
        if ( username_exists( $username ) ){
            $user = get_user_by( 'login', $username);
            update_user_meta( $user->ID, 'meta_key', $gebruiker['email'] );
            $users = wp_insert_user(
                'user_login'    =>  $gebruiker['username'],
                'user_pass'     =>  $gebruiker['password'],
                'first_name'    =>  $gebruiker['first_name'],
                'last_name'     =>  $gebruiker['last_name'],
                'user_email'    =>  $gebruiker['email'],
                'display_name'  =>  $gebruiker['first_name'] . ' ' . $gebruiker['last_name'],
                'nickname'      =>  $gebruiker['first_name'] . '' . $gebruiker['last_name'],
                'role'          =>  'subscriber'
            foreach ($data as $update_user) {   
                update_user_meta( $users, 'import_1', $update_user['first_name'] );
                update_user_meta( $users, 'import_2', $update_user['last_name'] );

To add files to the wp_usermeta table I want to work with update_user_meta since the wp_insert_user comes back with an ID of the just created user.

But no matter where I place the update_user_meta in the loop it gives me back the following error: Fatal error: Cannot use object of type WP_Error as array in
The error is fixed. Because the users allready existed. So need to check for that later. This does mean that the fields are added to the usermeta but empty

====== EDIT ======
The above code is final and working...

2 Answers 2


You can use wp_create_user and pass necessary information like $username, $password, $email , where $email is optional.

If user created successfully it will return ID of newly created user, other wise will return error object.

Then update user's other information using update_user_meta.

  • Wow that was exactly what I was reading here:tommcfarlin.com/add-custom-user-meta-during-registration I'll give it a try Commented Mar 31, 2015 at 10:48
  • Please see my edit. I'm so close but can't finish Commented Mar 31, 2015 at 13:14
  • your $user will be user id if created successfully. Then use update_user_meta. Commented Mar 31, 2015 at 13:56
  • I got it working now. See my updated code... Thnx for your help Commented Mar 31, 2015 at 14:24
  • Happy to help you, welcome :) Commented Apr 1, 2015 at 6:05

You can make use of CSV importer/exporter libraries to extract the CSV file data and save them as user details. FYI - always use wordpress functions to save anything. for ex. update_user_meta etc.

Cron you are using is correct and should run properly.

Library for extracting CSV data : goodby


try adding a error handling in your code:

//On success if( !is_wp_error($user) ) { echo "User created : ". $user; // update your meta here }

  • Thank you for your feed back. CronJob runs fine. I was hoping I could do this without a external Library. However I will take a look at it. I like the idea however to save data with the WP-functions instead of directly putting them in the database Commented Mar 30, 2015 at 10:46
  • I can't make anything of the library. But that is probably my problem because my knowledge doesn't reach there. can't I just import the data into fields and save them without the use of a button? Commented Mar 30, 2015 at 16:03
  • Yes. You can import the data into fields and save them with help of cron. Just make a cron script that will fetch the data from the csv file and save them accordingly. But for the same you have to use the csv library. Commented Mar 31, 2015 at 13:14
  • see this answers here stackoverflow.com/a/2805442/1576040. you can make use of php functions to extract csv data Commented Mar 31, 2015 at 13:16
  • Please read my edit so you know where I'm at right now! Commented Mar 31, 2015 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.