Action uncanny-toolkit-pro

uo_after_user_row_imported

Fires after a user row is successfully imported from a CSV file, providing access to user data and import details.

add_action( 'uo_after_user_row_imported', $callback, 10, 5 );

Description

Fires after a single user row has been successfully imported from CSV. Developers can use this hook to perform custom actions on the imported user, such as triggering additional logic, sending notifications, or updating custom meta data based on the user ID and CSV data.


Usage

add_action( 'uo_after_user_row_imported', 'your_function_name', 10, 5 );

Parameters

$user_id (mixed)
The ID of the user that was just imported.
$csv_array (mixed)
This parameter represents the ID of the user that was just imported or updated.
$csv_header (mixed)
This parameter contains an array representing the data for the current row being imported from the CSV file.
$key_location (mixed)
This parameter contains an array representing the header row of the CSV file, defining the column names.
$import_type (mixed)
This parameter indicates the specific key in the `$csv_array` that holds the value for the user's primary location.

Examples

add_action( 'uo_after_user_row_imported', 'my_custom_user_import_logic', 10, 5 );

/**
 * Example callback function for the uo_after_user_row_imported hook.
 * This function demonstrates how to perform additional actions after a user row has been imported.
 * For example, you might want to assign a specific role, add custom meta data, or trigger other processes.
 *
 * @param int    $user_id        The ID of the imported user.
 * @param array  $csv_row        The data from the current row of the CSV being processed.
 * @param array  $csv_header     The header row of the CSV file.
 * @param string $key_location   The key indicating the location of the user's identifier in the CSV.
 * @param string $import_type    The type of import being performed (e.g., 'users', 'memberships').
 */
function my_custom_user_import_logic( $user_id, $csv_row, $csv_header, $key_location, $import_type ) {

	// Ensure the user was successfully imported.
	if ( is_numeric( $user_id ) && $user_id > 0 ) {

		// Example 1: Assign a custom role if the import type is 'users' and a specific column indicates it.
		if ( 'users' === $import_type ) {
			$role_column_key = 'custom_role'; // Assuming 'custom_role' is a column in your CSV
			if ( isset( $csv_row[ $role_column_key ] ) && ! empty( $csv_row[ $role_column_key ] ) ) {
				$user_role = sanitize_text_field( $csv_row[ $role_column_key ] );
				$user      = get_user_by( 'id', $user_id );
				if ( $user ) {
					// Remove existing roles to ensure only the specified role is assigned (optional)
					$user->set_role( '' );
					// Add the custom role
					$user->add_role( $user_role );
					// You might want to log this action or add a success message.
					// error_log( "Assigned role '{$user_role}' to user ID {$user_id}." );
				}
			}
		}

		// Example 2: Add custom user meta based on another CSV column.
		$custom_meta_column_key = 'favorite_color'; // Assuming 'favorite_color' is a column in your CSV
		if ( isset( $csv_row[ $custom_meta_column_key ] ) && ! empty( $csv_row[ $custom_meta_column_key ] ) ) {
			$favorite_color = sanitize_text_field( $csv_row[ $custom_meta_column_key ] );
			update_user_meta( $user_id, 'user_favorite_color', $favorite_color );
			// error_log( "Added custom meta 'user_favorite_color' with value '{$favorite_color}' to user ID {$user_id}." );
		}

		// Example 3: Trigger another custom function for a specific import type.
		if ( 'course_enrollment' === $import_type ) {
			// Assume you have a function to enroll users in specific courses based on CSV data.
			// enroll_user_in_courses( $user_id, $csv_row, $csv_header );
		}
	}
}

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

if ( $email ) {
							$data['emails_sent'] += 1; //phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning
						} else {
							$data['ignored_rows_data'][ $i ] = esc_html__( 'Email failed to send.', 'uncanny-pro-toolkit' );
						}
					}
				}
				do_action( 'uo_after_user_row_imported', $user_id, $csv_array[ $i ], $csv_header, $key_location, $import_type );
			} else {
				// define error message
				$data['rows_ignored']            += 1; //phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning
				$data['ignored_rows_data'][ $i ] = $user_id->get_error_message();
			}
		}

Scroll to Top