Filter uncanny-automator

learndash_quiz_question_answer_postprocess

This filter is documented in includes/lib/wp-pro-quiz/wp-pro-quiz.php */ Filters the assessment object after a quiz question's answer is processed.

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

Description

This filter allows developers to modify the assessment data after a quiz question's answers have been processed. It's useful for altering answer display or adding custom information before the assessment is prepared for output.


Usage

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

Parameters

$assessment (mixed)
- **'assessment'** `mixed`

Return Value

The filtered value.


Examples

// Example: Add a custom prefix to the assessment text for a specific quiz.
function my_learndash_quiz_assessment_prefix( $assessment, $context ) {
    // Ensure this filter only affects the 'assessment' context and the specific quiz.
    if ( $context === 'assessment' && isset( $assessment['quiz_id'] ) && $assessment['quiz_id'] == 123 ) {
        // Assuming $assessment['data'] contains the answer content.
        // We'll iterate through the data and prepend a string.
        if ( ! empty( $assessment['data'] ) ) {
            foreach ( $assessment['data'] as $key => &$value ) {
                // Check if it's a relevant data field for the answer.
                if ( strpos( $key, '@@wpProQuizAssessment-' ) === 0 ) {
                    $value = 'Important Note: ' . $value;
                }
            }
            unset( $value ); // Unset the reference to avoid unexpected behavior.
        }
    }
    return $assessment;
}
add_filter( 'learndash_quiz_question_answer_postprocess', 'my_learndash_quiz_assessment_prefix', 10, 2 );

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

private function get_quiz_assessment_answer( $answer_text, $answer_data ) {

		$assessment = learndash_question_assessment_fetch_data( $answer_text, 0, 0 );
		if ( ! empty( $assessment['correct'] ) && is_array( $assessment['correct'] ) ) {

			$user_answer_index = absint( $answer_data[0] ) - 1;
			$answer            = '';

			foreach ( $assessment['correct'] as $answer_index => $answer_label ) {
				if ( $user_answer_index === $answer_index ) {
					$answer .= ' { ' . esc_html( $answer_label ) . ' } ';
				} else {
					$answer .= ' [ ' . esc_html( $answer_label ) . ' ] ';
				}
			}

			$answer_index                       = 0;
			$replace_key                        = "@@wpProQuizAssessment-{$answer_index}@@";
			$assessment['data'][ $replace_key ] = $answer;
			$assessment                         = learndash_question_assessment_prepare_output( $assessment );
			/** This filter is documented in includes/lib/wp-pro-quiz/wp-pro-quiz.php */
			$assessment = apply_filters( 'learndash_quiz_question_answer_postprocess', $assessment, 'assessment' );

			return do_shortcode( $assessment );
		}

		return '';
	}

Scroll to Top