uo_toolkit_csv_import_row_course_col
Fires after a course column is processed during CSV import, allowing modification of user IDs, course IDs, or row data.
add_action( 'uo_toolkit_csv_import_row_course_col', $callback, 10, 4 );
Description
Fires after LearnDash course IDs are processed during CSV import. Developers can use this hook to modify the $user_id, $course_ids, $current_row, or $key_location before courses are assigned to the user. This hook runs during the core CSV import process.
Usage
add_action( 'uo_toolkit_csv_import_row_course_col', 'your_function_name', 10, 4 );
Parameters
-
$user_id(mixed) - This parameter contains the ID of the user being imported.
-
$course_ids(mixed) - This parameter holds the ID of the user currently being processed during the CSV import.
-
$current_row(mixed) - This parameter contains an array of course IDs that have been identified for import.
-
$key_location(mixed) - This parameter contains the current row of data being processed from the CSV file.
Examples
add_action( 'uo_toolkit_csv_import_row_course_col', 'my_custom_course_enrolment_logic', 10, 4 );
/**
* Custom function to handle course enrollment based on CSV import data.
* This example demonstrates how to log successful course enrollments for a user.
*
* @param int $user_id The ID of the user being imported/updated.
* @param array $course_ids An array of all available Course IDs from LearnDash.
* @param array $current_row The current row being processed from the CSV import.
* @param array $key_location An array mapping column headers to their index in the current row.
*/
function my_custom_course_enrolment_logic( $user_id, $course_ids, $current_row, $key_location ) {
// Get the column index for 'learndash_courses' from the key_location.
$course_column_index = isset( $key_location['learndash_courses'] ) ? $key_location['learndash_courses'] : false;
// If the 'learndash_courses' column is not found in the mapping, exit early.
if ( ! $course_column_index ) {
return;
}
// Get the value from the current row for the course column.
$course_data_from_csv = isset( $current_row[ $course_column_index ] ) ? $current_row[ $course_column_index ] : '';
// If there's no course data in this row, we don't need to do anything.
if ( empty( $course_data_from_csv ) ) {
return;
}
// Assume the CSV course IDs are semicolon-separated.
$csv_course_ids = array_map( 'intval', explode( ';', $course_data_from_csv ) );
// Log the successful enrollment for demonstration purposes.
// In a real scenario, you might do more complex actions here.
foreach ( $csv_course_ids as $course_id ) {
if ( in_array( $course_id, $course_ids ) ) {
// Log the successful enrollment.
error_log( sprintf( 'User ID %d successfully enrolled in Course ID %d via CSV import.', $user_id, $course_id ) );
} else {
// Log if a provided course ID was not found or is invalid.
error_log( sprintf( 'User ID %d attempted enrollment in invalid Course ID %d via CSV import.', $user_id, $course_id ) );
}
}
}
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:1295
private static function perform_learndash_coursed_col_import( $user_id, $current_row, $key_location, $options, $course_ids ) {
$index = $key_location['learndash_courses'];
$index_exists = isset( $current_row[ $index ] );
$value = $index_exists ? $current_row[ $index ] : '';
$csv_course_ids = empty( $value ) ? $options['uo_import_enrol_in_courses'] : array_map( 'intval', explode( ';', $value ) );
// Remove values that are needed anymore so we can loop the rest as meta
if ( $index_exists ) {
unset( $current_row[ $index ] );
}
// Return data for the loop.
$results = array(
'current_row' => $current_row,
'index' => $index,
'value' => '',
);
if ( empty( $csv_course_ids ) ) {
return $results;
}
if ( self::$log_error ) {
$t1 = wp_json_encode( $csv_course_ids );
$t2 = wp_json_encode( $course_ids );
$log = "[ User id: {$user_id}] csv_course_ids: {$t1} course_ids: {$t2}n";
self::log_error( $log, 'enroll_course' );
}
foreach ( $csv_course_ids as $k => $csv_course_id ) {
if ( ! in_array( $csv_course_id, $course_ids ) ) {
unset( $csv_course_ids[ $k ] );
continue;
}
// add course access
if ( self::is_learndash_active() ) {
ld_update_course_access( $user_id, $csv_course_id );
} else {
add_user_meta( $user_id, 'import_user_csv_learndash_courses', $csv_course_id );
}
if ( self::$log_error ) {
$log = "[ User id: {$user_id}] csv_course_id: {$csv_course_id}n";
self::log_error( $log, 'enroll_course' );
}
}
// TODO REVIEW - shouldn't we pass the $csv_course_ids IDs that got added only?
do_action( 'uo_toolkit_csv_import_row_course_col', $user_id, $course_ids, $current_row, $key_location );
// Rejoin final $csv_course_ids results for CSV.
$results['value'] = implode(';', $csv_course_ids);
return $results;
}