uo_toolkit_csv_import_user_created
Fires after a user is successfully created during a CSV import.
add_action( 'uo_toolkit_csv_import_user_created', $callback, 10, 4 );
Description
Fires after a new WordPress user is successfully created during a CSV import. Developers can use this hook to perform actions such as assigning user roles, enrolling users in LearnDash courses, or updating custom user meta based on the imported data. It provides access to the created user object and the row data from the CSV.
Usage
add_action( 'uo_toolkit_csv_import_user_created', 'your_function_name', 10, 4 );
Parameters
-
$user_object(mixed) - The `$user_object` parameter contains the WordPress user object for the newly created user.
-
$current_row(mixed) - This parameter contains the `WP_User` object representing the newly created user in WordPress.
-
$csv_header(mixed) - This parameter holds the data from the current row being processed in the CSV file.
-
$key_location(mixed) - This parameter contains an array representing the header row of the CSV file, mapping column names to their respective order.
Examples
add_action( 'uo_toolkit_csv_import_user_created', 'my_custom_user_creation_logic', 10, 4 );
/**
* Example function to hook into the 'uo_toolkit_csv_import_user_created' action.
* This function demonstrates how to add custom logic after a user is created via the CSV import.
*
* @param WP_User $user_object The WP_User object of the newly created user.
* @param array $current_row The data from the current row in the CSV being processed.
* @param array $csv_header The header row from the CSV file.
* @param array $key_location An array mapping CSV header keys to their column index.
*/
function my_custom_user_creation_logic( $user_object, $current_row, $csv_header, $key_location ) {
// Example: Add a custom meta field to the user based on CSV data.
// Let's assume there's a column named 'customer_tier' in the CSV.
$customer_tier_key = array_search( 'customer_tier', $csv_header );
if ( $customer_tier_key !== false && isset( $current_row[ $customer_tier_key ] ) ) {
$customer_tier_value = sanitize_text_field( $current_row[ $customer_tier_key ] );
// Only add meta if the value is not empty
if ( ! empty( $customer_tier_value ) ) {
update_user_meta( $user_object->ID, 'customer_tier_level', $customer_tier_value );
error_log( "Added 'customer_tier_level' meta for user ID: {$user_object->ID} with value: {$customer_tier_value}" );
}
}
// Example: Assign a specific WordPress role to new users if a certain condition is met.
// Let's assume there's a column named 'account_type' in the CSV.
$account_type_key = array_search( 'account_type', $csv_header );
if ( $account_type_key !== false && isset( $current_row[ $account_type_key ] ) ) {
$account_type_value = sanitize_text_field( $current_row[ $account_type_key ] );
if ( 'premium' === strtolower( $account_type_value ) ) {
// Ensure the 'subscriber' role exists before attempting to add it.
if ( ! in_array( 'subscriber', $user_object->roles ) ) {
$user_object->add_role( 'subscriber' );
error_log( "Assigned 'subscriber' role to user ID: {$user_object->ID} due to premium account type." );
}
}
}
// You can perform any other custom actions here, such as:
// - Sending a custom welcome email.
// - Enrolling the user in a LearnDash course (if LearnDash is active).
// - Triggering other plugins or services.
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/import-learndash-users-from-csv.php:1035
if ( self::$log_error ) {
$encode_user_data = wp_json_encode( $userdata );
$log = "[ User id: {$user_id}] encode_user_data: {$encode_user_data} password: {$password}n";
self::log_error( $log, 'new_user' );
}
$user_object = get_user_by( 'ID', $user_id );
do_action( 'uo_toolkit_csv_import_user_created', $user_object, $current_row, $csv_header, $key_location );
} else {
// Check if updating is allowed
if ( 'update' !== $options['uo_import_existing_user_data'] ) {
$data['rows_ignored'] += 1; //phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning
$data['ignored_rows_data'][ $i ] = esc_html__( 'User Exists, option to update users is off', 'uncanny-pro-toolkit' );
continue;