uo_toolkit_csv_import_current_row
Filters the current row data during CSV import, allowing modification of the imported array and header.
add_filter( 'uo_toolkit_csv_import_current_row', $callback, 10, 3 );
Description
Fires for each row being processed during CSV import. Allows developers to modify the current row's data, including user details, before it's imported. Useful for data validation, transformation, or custom import logic. The hook receives the row data, header, and key location for context.
Usage
add_filter( 'uo_toolkit_csv_import_current_row', 'your_function_name', 10, 3 );
Parameters
-
$csv_array(mixed) - This parameter contains the data for the current row being processed in the CSV import.
-
$csv_header(mixed) - This parameter contains the data for the current row being processed in the CSV import.
-
$key_location(mixed) - This parameter contains an array representing the header row of the CSV file, which is used to map column names to their corresponding data.
Return Value
The filtered value.
Examples
<?php
/**
* Example: Modify the user email in the CSV import row if it's a specific domain.
*
* This filter allows you to intercept the current row data during a CSV import
* and make modifications. In this example, we'll check if the user's email
* belongs to a specific domain (e.g., 'example.com') and, if so, append a
* suffix to it. This could be useful for testing or staging environments.
*
* @param array $csv_array The current row data as an associative array.
* @param array $csv_header The header row from the CSV.
* @param array $key_location An array mapping CSV column headers to their keys.
*
* @return array The potentially modified CSV row data.
*/
add_filter(
'uo_toolkit_csv_import_current_row',
function ( $csv_array, $csv_header, $key_location ) {
// Ensure the required keys exist before proceeding.
if ( ! isset( $key_location['user_email'] ) || ! isset( $csv_array[ $key_location['user_email'] ] ) ) {
return $csv_array;
}
$user_email = $csv_array[ $key_location['user_email'] ];
$target_domain = 'example.com'; // The domain to target for modification.
$domain_suffix = '-test'; // The suffix to append to the email.
// Check if the email address contains the target domain.
if ( strpos( $user_email, '@' . $target_domain ) !== false ) {
// Extract the username part.
$email_parts = explode( '@', $user_email );
if ( count( $email_parts ) === 2 ) {
$username = $email_parts[0];
// Construct the modified email address.
$modified_email = $username . $domain_suffix . '@' . $target_domain;
// Update the email in the CSV array.
$csv_array[ $key_location['user_email'] ] = $modified_email;
// Optional: Log or mark this row for attention.
// For demonstration purposes, we won't add to the ignored rows here,
// but you could use do_action() or add to a temporary array.
// error_log( "Modified email for row: " . print_r( $csv_array, true ) );
}
}
return $csv_array;
},
10, // Priority: Default priority.
3 // Accepted Args: The number of arguments the callback function accepts.
);
?>
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:936
$row_queue = $imported_rows + 9;
for ( $i = $imported_rows; $i <= $row_queue; $i ++ ) {
if ( $i >= $total_rows ) {
break;
}
$current_row = apply_filters( 'uo_toolkit_csv_import_current_row', $csv_array[ $i ], $csv_header, $key_location );
$_email = trim( $current_row[ $key_location['user_email'] ] );
do_action( 'uo_toolkit_csv_import_before_row_import', $current_row, $csv_header, $key_location );
// check if email is proper
if ( ! is_email( stripcslashes( $_email ) ) ) {
$data['rows_ignored'] += 1; //phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning
$data['ignored_rows_data'][ $i ] = esc_html__( 'Malformed Email', 'uncanny-pro-toolkit' );