uo_toolkit_csv_import_row_wp_role_col
Fires after processing a user's role during CSV import, allowing customization of user role assignment based on row data.
add_action( 'uo_toolkit_csv_import_row_wp_role_col', $callback, 10, 3 );
Description
Fires after a user's WordPress role has been processed during CSV import. Developers can use this hook to perform custom actions based on the imported role, such as assigning additional capabilities or triggering specific user meta updates. Note that this hook is for internal plugin use and may change without notice.
Usage
add_action( 'uo_toolkit_csv_import_row_wp_role_col', 'your_function_name', 10, 3 );
Parameters
-
$user_id(mixed) - The `$user_id` parameter contains the ID of the user currently being processed for import.
-
$current_row(mixed) - This parameter contains the ID of the user being imported.
-
$key_location(mixed) - This parameter represents the current row of data being processed from the CSV file.
Examples
// This action hook is triggered after the default WordPress role column has been processed during a CSV import.
// It allows developers to hook in and perform custom actions related to the user's role assignment.
add_action( 'uo_toolkit_csv_import_row_wp_role_col', function( $user_id, $current_row, $key_location ) {
// Example: If a specific custom role is found in the CSV, add a meta key to the user.
// This could be used for custom logic or permissions based on that role.
// Check if the 'wp_role' key exists in the key_location array.
// This is a safety check to ensure we're dealing with a valid structure.
if ( isset( $key_location['wp_role'] ) ) {
// Get the column name for the WordPress role from $key_location.
$role_column_name = $key_location['wp_role'];
// Check if the role column exists in the current row data.
if ( isset( $current_row[ $role_column_name ] ) ) {
// Retrieve the role assigned to the user from the current row.
$assigned_role = $current_row[ $role_column_name ];
// Define a specific custom role we want to target for additional actions.
$target_custom_role = 'special_customer';
// Check if the user has been assigned the target custom role.
// We need to get the WP_User object to check their existing roles.
$wp_user = new WP_User( $user_id );
// Verify if the user actually has the $target_custom_role assigned.
if ( in_array( $target_custom_role, $wp_user->roles ) ) {
// If the user has the special role, add a custom meta key.
// This meta key could be used later by other plugins or themes.
update_user_meta( $user_id, 'has_special_customer_access', true );
// Optionally, you could log this action or notify someone.
// error_log( "User {$user_id} was assigned the '{$target_custom_role}' role. Added 'has_special_customer_access' meta." );
}
}
}
}, 10, 3 ); // Priority 10, accepts 3 arguments ($user_id, $current_row, $key_location).
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:1496
private static function perform_wp_role_col_import( $user_id, $current_row, $key_location, $options ) {
if ( isset( $current_row[ $key_location['wp_role'] ] ) ) {
if ( '' === $current_row[ $key_location['wp_role'] ] ) {
$csv_role = $options['uo_import_set_roles'][0];
} else {
$csv_role = $current_row[ $key_location['wp_role'] ];
}
if ( current_user_can( 'manage_options' ) && 'administrator' !== (string) $csv_role ) {
unset( $current_row[ $key_location['wp_role'] ] );
$wp_user = new WP_User( $user_id );
$uo_csv_overwrite_existing_roles = apply_filters( 'uo_csv_overwrite_existing_roles', true );
if ( ! $uo_csv_overwrite_existing_roles ) {
$wp_user->add_role( $csv_role );
} else {
$wp_user->set_role( $csv_role );
}
}
// Remove values that are needed anymore so we can loop the rest as meta
unset( $current_row[ $key_location['wp_role'] ] );
}
do_action( 'uo_toolkit_csv_import_row_wp_role_col', $user_id, $current_row, $key_location );
return $current_row;
}