Skip to content

Instantly share code, notes, and snippets.

@VR51
Last active August 9, 2021 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VR51/bd90a1dabfa32a90a122ff8760fd0fd3 to your computer and use it in GitHub Desktop.
Save VR51/bd90a1dabfa32a90a122ff8760fd0fd3 to your computer and use it in GitHub Desktop.
Import scheduler for the plugin WP User Imports
<?php
/*
# v1.0.1
# 2021-08-06
#
# Extends WP plugin 'Import Users from CSV' to enable scheduled WordPress user imports
#
# This scheduler has been rolled into my fork of the Import Users from CSV plugin
# See https://github.com/VR51/import-users-from-csv
#
# This Gist ONLY works when the plugin 'Import Users from CSV' is installed into WordPress
# Plugin URL: https://github.com/andrewlimaza/import-users-from-csv
# If that plugin is not installed this Gist will do nada.
#
# Read the comments to understand how to use this code snippet
*/
/**
* Automate/Schedule userdata and usermeta imports using the plugin
* 'Import Users from CSV'
* See the plugin code here https://github.com/andrewlimaza/import-users-from-csv
*
* 1. Put this snippet into a functionality plugin or put into your child theme's functions.php file
* 2. Configure the import options for $import_file_location and $args. See code comments to find these variables
* 3. Load a page of your website to schedule a time for the cron job to first run
* 4. The cron job is set once only when a frontend page of your site is first loaded after this code is installed.
* 5. The cron job will run twice daily. This can be configured. See code comments to see how to configure the cron job
*
* TIP: Add the code to your site at the time of day that you want the file import to run each day.
* The scheduled autorun time will be the same time of day that this task first runs
*
* Coded by Lee A Hodson of VR51.com
*
*/
function run_user_import_vr51() {
// Confirm Import Users from CSV is installed and active
if ( class_exists('IS_IU_Import_Users') ) {
// Configure location of CSV file that is to be imported
$import_file_location = 'https://example.com/filecsv';
// Configure plugin options
$args[] = array(
'password_nag' => false, // Show password nag? true (1) or false (0)
'new_user_notification' => false, // Send email notification to new users? true (1) or false (0)
'users_update' => true // Update user profiles if username or email exists? true (1) or false (0)
);
// Optional: Configure name of directory within wp-uploads that the CSV file will import into
$dirname = 'user-import/';
// Nothing else for you to configure in this function
// Download and store the CSV file that will be imported
// This step is not necessary in all cases but some servers and programs.. meh!
$upload_dir = wp_upload_dir();
$dir = trailingslashit( $upload_dir['basedir'] ) . $direname;
$upload_dir = wp_upload_dir();
$dir = trailingslashit( $upload_dir['basedir'] ) . $dirname;
$file = $dir . "import.csv";
// Fetch and save the CSV file. The file is stored with name 'import.csv' in the directory wp-uploads/user-import/
// require_once( ABSPATH . 'wp-admin/includes/file.php' ); // uncomment if this file is needed in your instance e.g this code is in a functionality plugin
global $wp_filesystem;
WP_Filesystem();
// Create $dir if it needs to be created
if( ! $wp_filesystem->is_dir( $dir ) ) {
$wp_filesystem->mkdir( $dir );
}
// Delete previously imported file if one exists
unlink( $filename );
// Fetch the import $csv file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $import_file_location);
curl_setopt($ch, CURLOPT_TRANSFERTEXT, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$csv = curl_exec($ch);
curl_close($ch);
// Import the new file and set file permissions to 0644
$wp_filesystem->put_contents( $dir . "import.csv", $csv, 0644 );
// Call the plugin Import Users from CSV
$import_users_vr51 = new IS_IU_Import_Users();
// Send file and configs to Import Users from CSV
$import_users_vr51->import_csv($file, $args);
}
}
// Set cron task to make this import run automatically
add_action( 'cron_run_user_import_vr51', 'run_user_import_vr51' );
// This next section can safely be commented out after the first import has successfully run
// but you would need to uncoment it if you ever clearout your cron jobs
// Configure the periodicity if you want: hourly, daily, twicedaily.
// See https://developer.wordpress.org/reference/functions/wp_schedule_event/
if ( ! wp_next_scheduled( 'cron_run_user_import_vr51' ) ) {
wp_schedule_event( time(), 'twicedaily', 'cron_run_user_import_vr51' );
}
// To unschedule the import, uncomment these 2 lines
// Do this if you uninstall WP User Imports
// $timestamp = wp_next_scheduled( 'cron_run_user_import_vr51' );
// wp_unschedule_event( $timestamp, 'cron_run_user_import_vr51' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment