Filter uncanny-automator

learndash_quiz_question_cloze_answers_to_lowercase

This filter is documented in includes/lib/wp-pro-quiz/wp-pro-quiz.php */ Filters whether the answer options for LearnDash cloze-style quiz questions are converted to lowercase.

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

Description

This filter controls whether LearnDash converts cloze test answers to lowercase for comparison. By default, it's enabled. Developers can return `false` to disable lowercase conversion, allowing case-sensitive comparisons for cloze question answers.


Usage

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

Return Value

The filtered value.


Examples

<?php
/**
 * Example of using the learndash_quiz_question_cloze_answers_to_lowercase filter.
 *
 * This example demonstrates how to disable the automatic conversion of cloze
 * question answers to lowercase when checking them against user input.
 *
 * @param bool $convert_to_lowercase The default value, true, indicates answers should be converted to lowercase.
 * @return bool False to disable lowercase conversion, true to keep it enabled.
 */
function my_learndash_disable_cloze_lowercase( $convert_to_lowercase ) {
    // By default, LearnDash converts cloze answers to lowercase for case-insensitive matching.
    // If you need case-sensitive matching for your cloze answers, return false.
    // For example, if the correct answer is "New York" and the user types "new york",
    // without this filter returning false, it would be considered correct.
    // Returning false here would make the comparison case-sensitive.
    return false;
}
add_filter( 'learndash_quiz_question_cloze_answers_to_lowercase', 'my_learndash_disable_cloze_lowercase', 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:1259

private function get_quiz_cloze_answer( $answer_text, $answer_data ) {

		$question_cloze_data = learndash_question_cloze_fetch_data( $answer_text );
		$answer_data_check   = array_map( 'trim', $answer_data );
		/** This filter is documented in includes/lib/wp-pro-quiz/wp-pro-quiz.php */
		if ( apply_filters( 'learndash_quiz_question_cloze_answers_to_lowercase', true ) ) {
			$lower_function    = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
			$answer_data_check = array_map( $lower_function, $answer_data_check );
		}
		foreach ( $question_cloze_data['correct'] as $correct_key => $correct_set ) {
			$correct_value = '---';
			if ( ( is_array( $correct_set ) ) && ( ! empty( $correct_set ) ) ) {
				if ( ! empty( $answer_data_check[ $correct_key ] ) ) {
					$correct_value = $answer_data_check[ $correct_key ];
				}
			}
			$replace_key                  = "@@wpProQuizCloze-{$correct_key}@@";
			$data['correct'][]            = $correct_value;
			$data['data'][ $replace_key ] = '{' . esc_html( $correct_value ) . '}';
		}

		if ( isset( $question_cloze_data['replace'] ) ) {
			$data['replace'] = $question_cloze_data['replace'];
		}

		return learndash_question_cloze_prepare_output( $data );
	}

Scroll to Top