Skip to main content
2 of 6
added 8 characters in body; edited tags
cybmeta
  • 20.6k
  • 5
  • 47
  • 56

Import users once a day without a plugin

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
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

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?

M.

Interactive
  • 1k
  • 3
  • 16
  • 36