Action
uncanny-automator-pro
automator_learndash_quiz_question_answered
Fires after a LearnDash quiz question is answered, providing its correctness, question ID, and quiz ID.
add_action( 'automator_learndash_quiz_question_answered', $callback, 10, 3 );
Description
Fires after a LearnDash quiz question is answered. Developers can use this hook to trigger custom actions based on whether the question was answered correctly, the question ID, and the quiz ID. This hook is invaluable for building advanced LearnDash integrations and custom quiz reporting.
Usage
add_action( 'automator_learndash_quiz_question_answered', 'your_function_name', 10, 3 );
Parameters
-
$correct(mixed) - This parameter indicates whether the user's answer to the question was correct.
-
$question_id(mixed) - This parameter indicates whether the user answered the question correctly.
-
$quiz_id(mixed) - This parameter contains the ID of the specific question that was answered in the LearnDash quiz.
Examples
add_action(
'automator_learndash_quiz_question_answered',
function( $correct, $question_id, $quiz_id ) {
// Example: Log the quiz question answer event for debugging or auditing.
// In a real-world scenario, you might trigger another action,
// update user meta, or send a notification based on the result.
if ( is_admin() ) {
// Avoid logging in the admin area if it's not relevant for admin users.
return;
}
$user_id = get_current_user_id();
if ( ! $user_id ) {
// If no user is logged in, we can't associate this with a specific user.
return;
}
$quiz_post = get_post( $quiz_id );
$quiz_title = $quiz_post ? $quiz_post->post_title : 'Unknown Quiz';
$question_post = get_post( $question_id );
$question_title = $question_post ? $question_post->post_title : 'Unknown Question';
$answer_status = $correct ? 'Correct' : 'Incorrect';
// Log the event to the WordPress debug log (if WP_DEBUG is enabled).
// For a production environment, consider a more robust logging solution.
error_log(
sprintf(
'User %d (%s) answered question "%s" (ID: %d) in quiz "%s" (ID: %d) as %s.',
$user_id,
get_userdata( $user_id )->user_login,
$question_title,
$question_id,
$quiz_title,
$quiz_id,
$answer_status
)
);
// Example of triggering another action based on the answer.
// If the question was answered incorrectly, you might want to flag it for review.
if ( ! $correct ) {
// You would replace this with a real call to another function or hook.
// For demonstration, we'll just log it.
error_log( sprintf( 'Question %d in quiz %d was answered incorrectly by user %d.', $question_id, $quiz_id, $user_id ) );
}
},
10, // Priority
3 // Accepted args: $correct, $question_id, $quiz_id
);
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
uncanny-automator-pro/src/integrations/learndash/handlers/learndash-handle-hooks.php:40
public function ld_quiz_question_answered( $results, $quiz, $question_models ) {
if ( empty( $results ) ) {
return;
}
$quiz_id = $quiz->getPostId();
$questions = array();
/** @var WpProQuiz_Model_Question $question_model */
foreach ( $question_models as $question_model ) {
$questions[ $question_model->getId() ] = $question_model->getQuestionPostId();
}
foreach ( $results as $q_id => $result ) {
$correct = isset( $result['c'] ) ? boolval( $result['c'] ) : false;
$question_id = isset( $questions[ $q_id ] ) ? absint( $questions[ $q_id ] ) : 0;
do_action( 'automator_learndash_quiz_question_answered', $correct, $question_id, $quiz_id );
}
}