Browse: Home / Hooks /

learndash_quiz_attempts

apply_filters( 'learndash_quiz_attempts',  boolean $attempts_left,  int $attempts_count,  int $user_id,  int $quiz_id )

Filters the quiz attempts left for a user.


Description #

See example https://developers.learndash.com/hook/learndash_quiz_attempts/


Parameters #

$attempts_left

(boolean) Whether any quiz attempts left for a user or not.

$attempts_count

(int) Number of Quiz attempts already taken.

$user_id

(int) ID of User taking Quiz.

$quiz_id

(int) ID of Quiz being taken.


Source #

File: includes/shortcodes/ld_quiz.php


Examples #

Note: Extended code example below not guaranteed, you may need to consult with a developer

 <?php
/**
 * Example usage for learndash_quiz_attempts filter.
 */

add_filter(
	'learndash_quiz_attempts',
	function( $attempts_left = 0, $attempts_count = 0, $user_id = 0, $quiz_id = 0 ) {

		// Not necessarily an "admin" but a common indicator if they can manage_options
		if ( current_user_can( 'manage_options' ) ) {
			$attempts_left = 1;
		}

		return $attempts_left;

	},
	30,
	4
);

add_filter(
	'learndash_quiz_attempts',
	function( $attempts_left = false, $attempts_count = 0, $user_id = 0, $quiz_id = 0 ) {
		if ( ! $attempts_left ) {
			// Example Only allow total $attempts_count to be less than 2.
			// And specific $user_id and $quiz_id.
			if ( ( 2 > $attempts_count ) && ( 529 === $user_id ) && ( 2858 === $quiz_id ) ) {
				$attempts_left = true;
			}
		}

		// Always return $attempts_left
		return $attempts_left;
	},
	30,
	4
);
 

Changelog #

Changelog
Version Description
3.1.0 Introduced.