Filter uncanny-automator

automator_learndash_user_quiz_questions_and_answers_unformatted_token_enclosure

Filters the enclosure characters used for unformatted LearnDash quiz questions and answers tokens.

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

Description

This filter hook, automator_learndash_user_quiz_questions_and_answers_unformatted_token_enclosure, fires when preparing unformatted LearnDash quiz question and answer data for use in tokens. Developers can modify the enclosure character used to wrap individual question/answer pairs. The default enclosure is a double quote (").


Usage

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

Return Value

The filtered value.


Examples

/**
 * Change the enclosure character for unformatted Learndash quiz questions and answers token.
 *
 * This filter allows you to modify the character used to enclose individual
 * question and answer strings when the 'automator_learndash_user_quiz_questions_and_answers_unformatted'
 * token is used. For example, if you want to use single quotes instead of double quotes.
 *
 * @param string $enclosure The default enclosure character, which is a double quote (").
 *
 * @return string The modified enclosure character.
 */
add_filter(
	'automator_learndash_user_quiz_questions_and_answers_unformatted_token_enclosure',
	function( $enclosure ) {
		// Example: Change the enclosure to single quotes.
		return "'";
	},
	10, // Priority: default is 10
	1  // Accepted args: only the enclosure value is passed
);

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

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