Filter
uncanny-automator
automator_learndash_user_quiz_questions_and_answers_token
Filters user quiz questions and answers for LearnDash before tokenization.
add_filter( 'automator_learndash_user_quiz_questions_and_answers_token', $callback, 10, 3 );
Description
This filter hook provides an array of a user's quiz questions and their answers for a specific LearnDash quiz. Developers can modify this array to alter the data passed to automations, allowing for custom data retrieval or modification based on user and quiz context.
Usage
add_filter( 'automator_learndash_user_quiz_questions_and_answers_token', 'your_function_name', 10, 3 );
Parameters
-
$questions_and_answers(mixed) - This parameter contains the array of questions and their corresponding answers for a specific user and quiz.
-
$user_id(mixed) - This parameter contains an array of questions and their corresponding answers for a specific user and quiz.
-
$quiz_post_id(mixed) - This parameter contains the unique identifier of the user for whom the quiz data is being retrieved.
Return Value
The filtered value.
Examples
add_filter( 'automator_learndash_user_quiz_questions_and_answers_token', function( $questions_and_answers, $user_id, $quiz_post_id ) {
// Example: If a specific quiz is being accessed, let's add a custom note to the user's answers.
// Replace 'YOUR_SPECIFIC_QUIZ_ID' with the actual post ID of the quiz you want to target.
$specific_quiz_id = YOUR_SPECIFIC_QUIZ_ID; // Make sure to define this constant or variable.
if ( $quiz_post_id === $specific_quiz_id ) {
// You might want to check if $questions_and_answers is an array and not empty.
if ( is_array( $questions_and_answers ) && ! empty( $questions_and_answers ) ) {
// Let's assume $questions_and_answers is an array of questions, and each question
// might have an 'answers' key which is an array of submitted answers.
// This is a hypothetical structure and might need adjustment based on the actual
// $questions_and_answers format returned by the plugin.
foreach ( $questions_and_answers as &$question_data ) {
if ( isset( $question_data['answers'] ) && is_array( $question_data['answers'] ) ) {
foreach ( $question_data['answers'] as &$answer_data ) {
// Add a custom note to each answer.
// Again, adjust 'user_note' based on where you'd want to store this.
// For simplicity, we're adding it directly. In a real scenario,
// you might want a more structured way to add metadata.
$answer_data['user_note'] = "Custom note for specific quiz access by user {$user_id}";
}
}
}
unset($question_data, $answer_data); // Unset references to avoid unintended side effects.
}
}
// Return the modified or original questions and answers.
return $questions_and_answers;
}, 10, 3 ); // Priority 10, accepts 3 arguments.
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:962
src/integrations/learndash/tokens/ld-tokens.php:1148
* @return array
*/
public function get_user_quiz_questions_and_answers( $user_id, $quiz_post_id ) {
// Take Results from Cache if available.
static $questions_and_answers = array();
if ( isset( $questions_and_answers[ $user_id ][ $quiz_post_id ] ) ) {
return apply_filters(
'automator_learndash_user_quiz_questions_and_answers_token',
$questions_and_answers[ $user_id ][ $quiz_post_id ],
$user_id,
$quiz_post_id
);
}