Action uncanny-toolkit-pro

uo_toolkit_csv_import_row_user

Fires after a user row is processed and imported from a CSV file during the import process.

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

Description

Fires after a user row has been processed during CSV import but before potential success actions. Developers can hook into this action to perform custom actions based on the imported user object, the current row data, CSV header, and key location. Useful for extending import functionality or logging specific row outcomes.


Usage

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

Parameters

$user_object (mixed)
This parameter contains the WordPress user object that has been updated or created during the CSV import process.
$current_row (mixed)
This parameter contains the WordPress user object that has been processed or updated during the CSV import.
$csv_header (mixed)
This parameter contains an array representing the current row of data being processed from the CSV file.
$key_location (mixed)
This parameter contains an array representing the header row of the CSV file, which maps column names to their index.

Examples

<?php
/**
 * Example callback function for the 'uo_toolkit_csv_import_row_user' action hook.
 * This function demonstrates how to process each user row during a CSV import.
 * It logs the user ID and username if the user is successfully imported or updated.
 *
 * @param WP_User $user_object       The WP_User object for the current user.
 * @param array   $current_row       An associative array representing the current row from the CSV.
 * @param array   $csv_header        An array containing the CSV header row.
 * @param int     $key_location      The index of the user identifier column in the CSV header.
 */
add_action( 'uo_toolkit_csv_import_row_user', function( $user_object, $current_row, $csv_header, $key_location ) {

	// Ensure we have a valid user object and an identifier
	if ( ! $user_object instanceof WP_User || empty( $current_row ) || ! isset( $csv_header[ $key_location ] ) ) {
		return;
	}

	$user_id   = $user_object->ID;
	$user_name = $user_object->user_login;
	$user_email = $user_object->user_email;

	// You could potentially check for specific columns in $current_row here
	// For example, to see if a course was assigned, or a role was changed.

	// Log a message for each successfully processed user row.
	// In a real-world scenario, you might use a more sophisticated logging mechanism
	// or perform actions like assigning courses, roles, or meta data.
	error_log( sprintf(
		'UO Toolkit CSV Import: Processed user row for User ID %d (%s, %s).',
		$user_id,
		$user_name,
		$user_email
	) );

	// Example: Assign a specific role to users if a certain column is present and set.
	$role_column_name = 'custom_role'; // Assuming 'custom_role' is a column in your CSV
	if ( isset( $current_row[ $role_column_name ] ) && ! empty( $current_row[ $role_column_name ] ) ) {
		$role_to_assign = sanitize_text_field( $current_row[ $role_column_name ] );

		// Check if the role is valid and exists in WordPress
		if ( array_key_exists( $role_to_assign, wp_roles()->roles ) ) {
			// Remove existing roles and add the new one
			$user_object->set_role( $role_to_assign );
			error_log( sprintf(
				'UO Toolkit CSV Import: Assigned role "%s" to User ID %d.',
				$role_to_assign,
				$user_id
			) );
		} else {
			error_log( sprintf(
				'UO Toolkit CSV Import: Invalid role "%s" specified for User ID %d. Role not assigned.',
				$role_to_assign,
				$user_id
			) );
		}
	}

}, 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:1130

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 ) ) {

				if ( 'new_user' === $import_type ) {

					$data['new_users'] += 1; //phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning


Scroll to Top