Action uncanny-toolkit-pro

uo_toolkit_csv_import_user_updated

Fires after a user has been updated during a CSV import.

add_action( 'uo_toolkit_csv_import_user_updated', $callback, 10, 4 );

Description

Fires after a LearnDash user has been successfully updated via CSV import. Developers can use this hook to perform custom actions, such as sending notifications, syncing data with external services, or adjusting user meta based on the imported data. It provides the updated user object, the current row from the CSV, the CSV header, and the key mapping.


Usage

add_action( 'uo_toolkit_csv_import_user_updated', 'your_function_name', 10, 4 );

Parameters

$user_object (mixed)
This parameter contains the WordPress user object for the user that was just updated.
$current_row (mixed)
This parameter contains the updated WordPress `WP_User` object for the user being processed in the CSV import.
$csv_header (mixed)
This parameter contains the data for the current row being processed from the CSV file.
$key_location (mixed)
This parameter contains an array of the header names from the CSV file, mapping column headers to their respective data.

Examples

/**
 * Example function to process user data after an update during CSV import.
 * This function demonstrates how to access and potentially modify or log
 * information related to the updated user, the current CSV row,
 * the CSV header, and the key location within the CSV.
 *
 * @param WP_User $user_object      The WP_User object of the updated user.
 * @param array   $current_row      An associative array representing the current row being processed from the CSV.
 * @param array   $csv_header       An array containing the header row of the CSV file.
 * @param array   $key_location     An associative array mapping CSV header keys to their column index in the current row.
 */
function my_custom_user_updated_handler( $user_object, $current_row, $csv_header, $key_location ) {

	// Ensure we have a valid user object before proceeding.
	if ( ! $user_object instanceof WP_User || ! $user_object->exists() ) {
		error_log( 'my_custom_user_updated_handler: Invalid or non-existent user object provided.' );
		return;
	}

	// Log some information about the updated user and the import process for debugging.
	$user_id = $user_object->ID;
	$username = $user_object->user_login;
	$user_email = $user_object->user_email;

	$log_message = sprintf(
		'User "%s" (ID: %d) was successfully updated via CSV import. Email: %s.',
		esc_html( $username ),
		absint( $user_id ),
		esc_html( $user_email )
	);

	// Example: Check if a specific custom field was updated and log its new value.
	$custom_field_key = 'custom_meta_field_to_track'; // Replace with an actual key from your CSV header.
	if ( isset( $key_location[ $custom_field_key ] ) ) {
		$column_index = $key_location[ $custom_field_key ];
		$new_value = $current_row[ $column_index ];
		$log_message .= sprintf( ' Custom field "%s" updated to: "%s".', esc_html( $custom_field_key ), esc_html( $new_value ) );

		// Example: Perform an additional action based on the updated custom field value.
		if ( 'active' === strtolower( $new_value ) ) {
			// Potentially send a welcome email or grant access to a specific course.
			// For demonstration, we'll just log another message.
			$log_message .= ' User marked as active.';
		}
	}

	// Log the information for review (e.g., in WordPress debug.log or a custom log file).
	error_log( $log_message );

	// Example: You could also update user meta here if needed, based on the imported data.
	// update_user_meta( $user_id, 'last_csv_import_date', current_time( 'mysql' ) );
}
add_action( 'uo_toolkit_csv_import_user_updated', 'my_custom_user_updated_handler', 10, 4 );

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

if ( self::$log_error ) {
					$encode_user_data = wp_json_encode( $userdata );
					$log              = "[ User id: {$user_id}] encode_user_data: {$encode_user_data} password: {$password}n";
					self::log_error( $log, 'update_user' );
				}

				$import_type = 'updated_user';
				do_action( 'uo_toolkit_csv_import_user_updated', $user_object, $current_row, $csv_header, $key_location );
			}

			do_action( 'uo_toolkit_csv_import_row_user', $user_object, $current_row, $csv_header, $key_location );

			//On success
			if ( ! is_wp_error( $user_id ) ) {


Scroll to Top