I have the following code to write a simple message to a log file when a user completes a LearnDash quiz. It does not seem to fire the action. Similar code for Lesson, Topic, and Course complete works fine but this does not. No PHP errors produced. It just fails silently.
Does adding an action to this LearnDash hook actually work or not? If so, what am I doing wrong? add_action( 'learndash_quiz_completed', function( $quizdata, $user )
I have tried the following code snippets:
function mjcs_process_quiz_completed( $quiz_data, $user ) {
if ( !file_exists( "logs" ) && !is_dir( "logs" ) ) {
mkdir( "logs" );
}
$file = fopen("logs/simple_logs.log","a");
$message = "\n Quiz completed ";
fwrite($file, $message );
fclose($file);
}
add_action( 'learndash_quiz_completed','mjcs_process_quiz_completed',15,2);
I have also tried this variant from the Developers documentation: https://developers.learndash.com/hook/learndash_quiz_completed/
add_action(
'learndash_quiz_completed',
function( $quizdata, $user ) {
// May add any custom logic using $quizdata, $user
if ( !file_exists( "logs" ) && !is_dir( "logs" ) ) {
mkdir( "logs" );
}
$file = fopen("logs/simple_logs.log","a");
$message = "Quiz completed ";
fwrite($file, $message );
fclose($file);
},
10,
2
);