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.
I changed my way of thinking and want to do the following:
$row = 1;
ini_set('auto_detect_line_endings',TRUE);
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";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
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"; ini_set('auto_detect_line_endings',TRUE); $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; $c++; } } else if($i > 0) { $c = 0; foreach($line as $col) { $data[$i][$cols[$c]] = $col; $c++; } } $i++; } foreach ($data as $user){ $user = wp_insert_user( array( 'user_login' => $user['username'], 'user_pass' => $user['password'], 'first_name' => $user['first_name'], 'last_name' => $user['last_name'], 'user_email' => $user['email'], 'display_name' => $user['first_name'] . ' ' . $user['last_name'], 'nickname' => $user['first_name'] . '' . $user['last_name'], 'role' => 'subscriber' ) ); }
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