Filter uncanny-automator

automator_learndash_user_quiz_questions_and_answers_unformatted_token_delimiter

Filters the delimiter used to separate quiz questions and answers when the token is unformatted.

add_filter( 'automator_learndash_user_quiz_questions_and_answers_unformatted_token_delimiter', $callback, 10, 1 );

Description

Filters the delimiter used when formatting LearnDash quiz questions and answers for an unformatted token. Developers can change this to any string to customize how individual question/answer pairs are separated in output, useful for custom integrations or reporting.


Usage

add_filter( 'automator_learndash_user_quiz_questions_and_answers_unformatted_token_delimiter', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Change the delimiter used for separating quiz questions and answers in unformatted tokens.
 *
 * By default, this filter uses a comma (',') as the delimiter. This example
 * demonstrates how to change it to a pipe symbol ('|') for better readability
 * when questions or answers might contain commas themselves.
 *
 * @param string $delimiter The current delimiter.
 * @return string The modified delimiter.
 */
add_filter( 'automator_learndash_user_quiz_questions_and_answers_unformatted_token_delimiter', function( $delimiter ) {
	// Change the delimiter from ',' to '|' for better handling of commas within questions/answers.
	return '|';
}, 10, 1 );

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/integrations/learndash/tokens/ld-tokens.php:1185

public function format_user_quiz_questions_and_answers_csv( $data ) {
		if ( empty( $data ) ) {
			return '';
		}

		// Format Data array.
		$array = array();
		foreach ( $data as $line ) {
			// Flatten Answers array to string.
			$answer = '';
			if ( ! empty( $line['answer'] ) ) {
				if ( count( $line['answer'] ) > 1 ) {
					$answer .= implode( ', ', $line['answer'] );
				} else {
					$answer .= $line['answer'][0];
				}
			}
			// Remove all HTML and add to array.
			$array[] = array(
				'question' => wp_strip_all_tags( $line['question'] ),
				'answer'   => wp_strip_all_tags( $answer ),
			);
		}

		$delimiter   = apply_filters(
			'automator_learndash_user_quiz_questions_and_answers_unformatted_token_delimiter',
			','
		);
		$enclosure   = apply_filters(
			'automator_learndash_user_quiz_questions_and_answers_unformatted_token_enclosure',
			'"'
		);
		$escape_char = apply_filters(
			'automator_learndash_user_quiz_questions_and_answers_unformatted_token_escape_char',
			'\'
		);

		return Utilities::array_to_csv( $array, $delimiter, $enclosure, $escape_char );
	}


Scroll to Top