uo_toolkit_csv_import_wp_insert_user
Filters the user data array before a user is inserted during a CSV import.
add_filter( 'uo_toolkit_csv_import_wp_insert_user', $callback, 10, 2 );
Description
Filters user data before WordPress inserts a new user during CSV import. Modify user details like email, username, or meta before creation. This hook is ideal for custom user data manipulation or validation during the import process.
Usage
add_filter( 'uo_toolkit_csv_import_wp_insert_user', 'your_function_name', 10, 2 );
Parameters
-
$userdata(mixed) - This parameter contains an array of user data that will be used to create or update a WordPress user.
-
$current_row(mixed) - This parameter contains an array of user data arguments intended for the `wp_insert_user` function.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the uo_toolkit_csv_import_wp_insert_user filter.
* This example demonstrates how to add custom user meta to a user before they are inserted into WordPress.
*
* @param array $userdata An array of user data to be passed to wp_insert_user().
* @param array $current_row The current row of data being processed from the CSV.
* @return array The modified $userdata array.
*/
add_filter( 'uo_toolkit_csv_import_wp_insert_user', function( $userdata, $current_row ) {
// Check if a custom meta field 'custom_import_id' exists in the current row data.
if ( isset( $current_row['custom_import_id'] ) && ! empty( $current_row['custom_import_id'] ) ) {
// Add the 'custom_import_id' value as user meta.
// The key for the user meta will be 'custom_imported_from_csv'.
$userdata['meta_input']['custom_imported_from_csv'] = sanitize_text_field( $current_row['custom_import_id'] );
}
// You could also modify existing user data, for example, to enforce a specific role.
// if ( !isset( $userdata['role'] ) || 'subscriber' !== $userdata['role'] ) {
// $userdata['role'] = 'editor'; // Force all imported users to be editors.
// }
// Always return the $userdata array, whether modified or not.
return $userdata;
}, 10, 2 );
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:1013
$userdata = apply_filters_deprecated(
'csv_wp_insert_user',
array( $userdata, $current_row ),
'3.7.12',
'uo_toolkit_csv_import_wp_insert_user'
);
$userdata = apply_filters( 'uo_toolkit_csv_import_wp_insert_user', $userdata, $current_row );
$user_id = wp_insert_user( $userdata );
$import_type = 'new_user';
if ( is_multisite() ) {
$current_blog_id = get_current_blog_id();
if ( isset( $userdata['role'] ) ) {