Filter uncanny-toolkit-pro

uo_toolkit_csv_import_delimiter

Filters the delimiter used for CSV imports when processing CSV input.

add_filter( 'uo_toolkit_csv_import_delimiter', $callback, 10, 2 );

Description

This filter allows you to modify the delimiter used when parsing CSV data during import. Developers can change the delimiter character to accommodate different CSV formats, ensuring correct parsing of user data. The hook receives the current delimiter and the raw CSV input for context.


Usage

add_filter( 'uo_toolkit_csv_import_delimiter', 'your_function_name', 10, 2 );

Parameters

$delimiter (mixed)
This parameter allows you to filter and change the delimiter character used to separate values within the CSV data being imported.
$csv_input (mixed)
This parameter contains the delimiter character used to parse the CSV data.

Return Value

The filtered value.


Examples

add_filter( 'uo_toolkit_csv_import_delimiter', 'my_custom_csv_delimiter', 10, 2 );

/**
 * Example function to override the default CSV delimiter.
 *
 * This function is attached to the 'uo_toolkit_csv_import_delimiter' hook.
 * It allows you to specify a custom delimiter for CSV imports if the automatic detection fails
 * or if you have a specific delimiter you need to enforce.
 *
 * In this example, we'll check if the detected delimiter is a semicolon (';') and
 * if so, we'll enforce it. Otherwise, we'll fall back to the detected delimiter.
 *
 * @param string $delimiter The detected CSV delimiter.
 * @param array  $csv_input The array of CSV rows.
 * @return string The chosen CSV delimiter.
 */
function my_custom_csv_delimiter( $delimiter, $csv_input ) {
    // Check if the detected delimiter is a semicolon, and if it's likely correct based on the first row.
    // This is a basic check; more robust logic might be needed depending on your CSV structure.
    if ( ';' === $delimiter && false !== strpos( $csv_input[0] ?? '', ';' ) ) {
        // If it's a semicolon and it appears in the first row, enforce it.
        return ';';
    }

    // If the above condition is not met, return the original detected delimiter.
    return $delimiter;
}

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

private static function get_csv( $csv_input ) {
		@ini_set( 'auto_detect_line_endings', '1' );
		$csv_input = str_replace( "rn", "n", $csv_input );
		$csv_input = str_replace( "r", "n", $csv_input );

		$csv_input = str_getcsv( $csv_input, "n" );

		$csv_input_temp = array();
		foreach ( $csv_input as $key => $row ) {
			if ( ! empty( $row ) ) {
				$csv_input_temp[] = $row;
			}
		}

		$csv_input = $csv_input_temp;

		unset( $csv_input_temp );

		// Auto-detect delimiter from first row
		$delimiter = self::detect_csv_delimiter( $csv_input[0] ?? '' );
		
		// Allow filter to override delimiter
		$delimiter = apply_filters( 'uo_toolkit_csv_import_delimiter', $delimiter, $csv_input );

		foreach ( $csv_input as $i => &$row ) {
			$row = str_getcsv( stripcslashes( $row ), $delimiter );

			// if the CSV row is empty discard it.
			if ( ! array_filter( $row ) ) {
				unset( $csv_input[ $i ] );
			}
		}

		return $csv_input;
	}

Scroll to Top