Action uncanny-toolkit-pro

uo_toolkit_csv_import_before_row_import

Fires before each row is imported, providing access to the current row data and CSV header.

add_action( 'uo_toolkit_csv_import_before_row_import', $callback, 10, 3 );

Description

Fires before a single row of user data is imported from a CSV. Developers can use this action to modify the `$current_row` data, access the `$csv_header` and `$key_location` for context, or halt the import process by returning `false` from a callback.


Usage

add_action( 'uo_toolkit_csv_import_before_row_import', 'your_function_name', 10, 3 );

Parameters

$current_row (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 the header row from your CSV file, which is used to map column names to their corresponding data.

Examples

/**
 * Example of how to use the uo_toolkit_csv_import_before_row_import action hook.
 * This example demonstrates how to add a custom validation rule to check if a user's
 * first name is present in the current row being imported.
 *
 * @param array $current_row The data for the current row being imported.
 * @param array $csv_header The header row of the CSV file.
 * @param array $key_location An array mapping CSV header keys to their expected locations.
 */
add_action( 'uo_toolkit_csv_import_before_row_import', function( $current_row, $csv_header, $key_location ) {
    // Assume 'first_name' is a key in $csv_header and $key_location
    // Check if 'first_name' key exists in key_location and the corresponding value in $current_row is empty
    if ( isset( $key_location['first_name'] ) && empty( $current_row[ $key_location['first_name'] ] ) ) {
        // If the first name is missing, log an error and prevent the row from being imported.
        // In a real scenario, you might want to access and modify a global $data array
        // or return a specific value that the main import function can interpret.
        // For this example, we'll simulate adding it to an ignored_rows_data array.

        // NOTE: Accessing and modifying global $data or returning values from action hooks
        // can be complex. This example simplifies by assuming the hook's context allows
        // for some form of error reporting. In a real implementation, you'd need to
        // understand how the main import function handles feedback from this hook.

        // For demonstration, let's assume $data is accessible and has 'rows_ignored' and 'ignored_rows_data' keys.
        // This would typically be managed within the main import function's scope.
        // global $data; // If $data is a global variable.

        // If $data is not global, you'd need a way to pass it back or modify it.
        // A common pattern is for the function calling the hook to check return values
        // or have access to shared variables.
        // Since this is an 'action' hook, direct return isn't possible for modifying
        // the import flow. You'd likely set flags or add to data structures managed
        // by the calling function.

        // Placeholder for how you might report an ignored row:
        // $data['rows_ignored']++;
        // $data['ignored_rows_data'][ key( $current_row ) ] = esc_html__( 'Missing First Name', 'your-text-domain' );

        error_log( 'Skipping row due to missing first name: ' . print_r( $current_row, true ) );

        // In a real plugin, you'd signal to the main import logic to skip this row.
        // This might involve setting a flag, or throwing an exception, or returning a specific value
        // if the hook were a filter. As an action, you'd typically rely on the calling code
        // to check for specific conditions or modifications you might make to shared variables.
    }
}, 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:939

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' );
				continue;
			}


Scroll to Top