Snippets

LearnDash LearnDash Filter CSV output object

Created by Paul Menard last modified
/**
 * LearnDash Filter CSV output object
 *
 * LearnDash uses the external PHP library parseCSV ( https://github.com/parsecsv/parsecsv-for-php )
 * when there is a need to create CSV report output. This filter provides a hook to allow tweaks to 
 * the CSV object. Some tweaks might be field delimeter from comma to semi-colon, or output encoding.
 *
 *
 * @args $csv_object object instance of the lmsParseCSV class
 * @args $context string This is the CSV output being generated. Possible values 'courses', 'quizzes' or 'transactions'
 * 
 * @return $csv_object The modified CSV object
 */
add_filter('learndash_csv_object', function ( $csv_object, $context = '' ) {
	
	// Example 1: Set the field delmieter from the default comma ',' to semi-colon ';'
	// This is the default needed for MS Excel in EU. 
	$csv_object->delimiter = ';';
	
    // Example 2: Change the encoding to something other than 'ISO-8859-1'.
    // This takes two parameters 1) inbound and 2) output.
    // Also adding '//IGNORE' aftee the encoding type can prevent errors in the conversion which might cause errors. 
    $csv_object->encoding('utf-8//IGNORE', 'utf-8//IGNORE');
        
	// Return the CSV object
	return $csv_object;
}, 10, 2);	

Comments (0)