Filter uncanny-toolkit-pro

uo_toolkit_csv_import_wp_user_by_field

Filters the WordPress user data when importing via CSV by a specific field before saving.

add_filter( 'uo_toolkit_csv_import_wp_user_by_field', $callback, 10, 2 );

Description

Filters the method used to identify an existing WordPress user during CSV import. Developers can modify the field (e.g., 'email', 'user_login') to look up users, or return a specific user ID. This hook fires when a row is processed and a potential user match is being sought.


Usage

add_filter( 'uo_toolkit_csv_import_wp_user_by_field', 'your_function_name', 10, 2 );

Parameters

$current_row (mixed)
This parameter contains the email address of the user being processed for import.
$key_location (mixed)
This parameter represents the current row of data being processed from the CSV file, containing information for a single user.

Return Value

The filtered value.


Examples

/**
 * Example: Modify how users are identified for import.
 *
 * This filter allows you to change the field used to identify an existing user
 * during the CSV import process. By default, it's set to 'email'. This example
 * demonstrates how you might conditionally switch to 'username' if a specific
 * column is present and contains data.
 *
 * @param string $user_by_field The field to use for finding an existing user (e.g., 'email', 'username').
 * @param array  $current_row   The current row of data being processed from the CSV.
 * @param array  $key_location  An array mapping CSV column headers to WordPress user fields.
 *
 * @return string The modified field to use for finding an existing user.
 */
add_filter( 'uo_toolkit_csv_import_wp_user_by_field', function( $user_by_field, $current_row, $key_location ) {
	// Check if the 'username' key exists in key_location and if it has a corresponding column in the current row.
	// Also, ensure that the username column in the current row actually contains data.
	if ( isset( $key_location['user_login'] ) && array_key_exists( $key_location['user_login'], $current_row ) && ! empty( $current_row[ $key_location['user_login'] ] ) ) {
		// If a username is available and populated, prioritize using username for finding the user.
		return 'username';
	}

	// Otherwise, fall back to the default or the previously determined $user_by_field.
	return $user_by_field;
}, 10, 3 );

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:1048

$data['ignored_rows_data'][ $i ] = esc_html__( 'User Exists, option to update users is off', 'uncanny-pro-toolkit' );
					continue;
				}

				// Emails exists, check if user updates are allow
				$password    = ( $key_location['user_pass'] ) ? $current_row[ $key_location['user_pass'] ] : '';

				$user_by_field = apply_filters( 'uo_toolkit_csv_import_wp_user_by_field', 'email', $current_row, $key_location );

				$user_object = false;
				$ignored_row_message = esc_html__( 'User not found.', 'uncanny-pro-toolkit' );
				if( 'email' === $user_by_field ) {
					$user_object = get_user_by( 'email', $_email );
					$ignored_row_message = esc_html__( 'User with this email not found.', 'uncanny-pro-toolkit' );
				} elseif( 'username' === $user_by_field ) {


Scroll to Top