Filter
uncanny-automator
automator_learndash_quiz_q_and_a_tokens
Filters the available LearnDash quiz tokens before they are displayed for use in automations.
add_filter( 'automator_learndash_quiz_q_and_a_tokens', $callback, 10, 1 );
Description
Fires when LearnDash quiz tokens are being prepared for question/answer triggers. Developers can use this filter to add or modify available quiz-related tokens, such as quiz completion status, score, or percentage. Ensure tokens provided are valid and contextually appropriate for LearnDash quizzes.
Usage
add_filter( 'automator_learndash_quiz_q_and_a_tokens', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Modify the LearnDash quiz question and answer tokens.
*
* This function allows developers to add, remove, or modify the available tokens
* for LearnDash quiz triggers within the Automator plugin.
*
* @param array $q_a_tokens An array of existing quiz question and answer tokens.
* @return array The modified array of tokens.
*/
function my_custom_learndash_quiz_tokens( $q_a_tokens ) {
// Add a new custom token if the user has completed the quiz and passed.
// This token might be used in a custom notification or action.
$q_a_tokens[] = 'LD_QUIZ_PASSED_WITH_HONORS';
// You could also remove existing tokens if they are not needed.
// For example, if you don't want to expose the raw score:
// $q_a_tokens = array_diff( $q_a_tokens, array( 'LD_QUIZSCORE' ) );
return $q_a_tokens;
}
add_filter( 'automator_learndash_quiz_q_and_a_tokens', 'my_custom_learndash_quiz_tokens', 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:658
public function possible_tokens_quiz_q_and_a( $tokens = array(), $args = array() ) {
if ( ! automator_do_identify_tokens() ) {
return $tokens;
}
if ( empty( $args['value'] ) || empty( $args['meta'] ) || empty( $args['triggers_meta'] ) ) {
return $tokens;
}
$quiz_id = (int) $args['value'];
// Bail early if not a quiz trigger or Quiz ID is less than or = 0 ( -1 )
if ( 'LDQUIZ' !== $args['meta'] || $quiz_id <= 0 ) {
return $tokens;
}
$q_a_triggers = apply_filters(
'automator_learndash_quiz_q_and_a_tokens',
array(
'LD_PASSQUIZ',
'LD_FAILQUIZ',
'LD_QUIZDONE',
'LD_QUIZPERCENT',
'LD_QUIZPOINT',
'LD_QUIZSCORE',
)
);
if ( ! in_array( $args['triggers_meta']['code'], $q_a_triggers, true ) ) {
return $tokens;
}
static $quiz_q_and_a_tokens = array();
// Generate Tokens for Quiz Questions and Answers.
if ( empty( $quiz_q_and_a_tokens[ $quiz_id ] ) ) {
$quiz_q_and_a_tokens[ $quiz_id ] = array();
$questions_ids = learndash_get_quiz_questions( $quiz_id );
if ( ! empty( $questions_ids ) ) {
foreach ( $questions_ids as $question_post_id => $question_pro_id ) {
$question_title = get_the_title( $question_post_id );
$length = strlen( $question_title );
$max = 42;
$question_title = substr( $question_title, 0, $max );
$question_title .= ( $length > $max ) ? '...' : '';
// Question Token.
$quiz_q_and_a_tokens[ $quiz_id ][] = array(
'tokenId' => 'LDQUIZ_QUESTION_ID_' . $question_post_id,
'tokenName' => sprintf(
/* translators: %d, Question Post ID %s: Question Token title */
_x( 'Question (%1$d) - %2$s', 'LearnDash Question Token', 'uncanny-automator' ),
$question_post_id,
$question_title
),
'tokenType' => 'text',
'tokenIdentifier' => 'LDQUIZ',
);
// Answer Token.
$quiz_q_and_a_tokens[ $quiz_id ][] = array(
'tokenId' => 'LDQUIZ_ANSWER_ID_' . $question_post_id,
'tokenName' => sprintf(
/* translators: %d, Question Post ID %s: Question Token title */
_x( 'Answer (%1$d) - %2$s', 'LearnDash Answer Token', 'uncanny-automator' ),
$question_post_id,
$question_title
),
'tokenType' => 'text',
'tokenIdentifier' => 'LDQUIZ',
);
}
}
}
// Merge Tokens.
if ( ! empty( $quiz_q_and_a_tokens[ $quiz_id ] ) ) {
$tokens = array_merge( $tokens, $quiz_q_and_a_tokens[ $quiz_id ] );
}
return $tokens;
}