apply_filters( 'learndash_quiz_content', string $quiz_content, WP_Post $quiz_post )
Filters ld_quiz shortcode content.
Description #
Parameters #
Source #
Examples #
Note: Extended code example below not guaranteed, you may need to consult with a developer
<?php
/**
* Example usage for learndash_quiz_content filter.
*/
add_filter(
'learndash_quiz_content',
function ( $quiz_content = '', WP_Post $quiz_post ) {
// Set this logic to only effect certain Quizzes. Leave blank for all.
$check_quiz_only = array( 107 );
// Set our threshold limit. See https://codex.wordpress.org/Easier_Expression_of_Time_Constants for handy time constants
$time_complete_threshold = 4 * HOUR_IN_SECONDS;
if ( ( ! empty( $quiz_post ) ) && ( ( empty( $check_quiz_only ) ) || ( in_array( $quiz_post->ID, $check_quiz_only ) === true ) ) ) {
$user_id = get_current_user_id();
$user_quiz_results = get_user_meta( $user_id, '_sfwd-quizzes', true );
if ( ! empty( $user_quiz_results ) ) {
// We reverse the user quizzes since newer items are added to the bottom.
$user_quiz_results = array_reverse( $user_quiz_results );
// Then we loop newest to oldest...
foreach ( $user_quiz_results as $user_quiz_result ) {
// ...until we find a match between the result quiz ID and the quiz post ID.
if ( $user_quiz_result['quiz'] == $quiz_post->ID ) {
// Check if the result quiz completed time is within out threshold.
if ( ( $user_quiz_result['completed'] + $time_complete_threshold ) > time() ) {
// If it is we display an alternate quiz content.
$quiz_content = '<p>Too soon. Try again in about ' . human_time_diff( ( $user_quiz_result['completed'] + $time_complete_threshold ), time() ) . '.</p>';
}
}
}
}
}
// Always return $quiz_content;
return $quiz_content;
},
10,
2
);
add_filter(
'learndash_quiz_content',
function ( $quiz_content = '', WP_Post $quiz_post ) {
$quiz_completed = learndash_is_quiz_complete( get_current_user_id(), $quiz_post->ID );
if ( $quiz_completed ) {
$quiz_content = '<p>You have already passed this Quiz.</p>';
}
// Always return $quiz_content;
return $quiz_content;
},
10,
2
);
Changelog #
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |