Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaubersilva/fa83843b0f487ff37753342904cc317d to your computer and use it in GitHub Desktop.
Save glaubersilva/fa83843b0f487ff37753342904cc317d to your computer and use it in GitHub Desktop.
[Forminator] Sends Quiz Right Answers by Email
<?php
/**
* Plugin Name: [Forminator] Sends Quiz Right Answers by Email
* Plugin URI: https://wpmudev.com/
* Description: The default email sent by the Forminator doesn’t show the correct answers to the quizzes. It only shows the answer which was answered by the quiz taker. This plugin makes the right answers also be displayed.
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://wpmudev.com/
* Task: SLS-2027
* License: GPLv2 or later
*
* @package WPMUDEV_Forminator_Sends_Quiz_Right_Answers_by_Email
*/
defined( 'ABSPATH' ) || exit;
function wpmudev_forminator_intercept_quiz_answers( $message, $quiz, $data ){
foreach ( $data['entry'] as $key => $value ) {
if ( $value['isCorrect'] ) {
$old = '<li><b>Question : </b>' . esc_html( $data['entry'][ $key ]['question'] ) . '</li>' . PHP_EOL .
'<li><b>Answer : </b>' . esc_html( $data['entry'][ $key ]['answer'] ) . '</li>';
$new = '<li><b>Question : </b>' . esc_html( $data['entry'][ $key ]['question'] ) . '</li>' . PHP_EOL .
'<li><b>Answer : </b>' . esc_html( $data['entry'][ $key ]['answer'] ) . ' - ✅ Right</li>';
$message = str_replace( $old, $new, $message );
} else {
foreach ( $quiz->questions[ $key ]['answers'] as $answer ) {
if ( isset( $answer['toggle'] ) && $answer['toggle'] ) {
$old = '<li><b>Question : </b>' . esc_html( $data['entry'][ $key ]['question'] ) . '</li>' . PHP_EOL .
'<li><b>Answer : </b>' . esc_html( $data['entry'][ $key ]['answer'] ) . '</li>';
$new = '<li><b>Question : </b>' . esc_html( $data['entry'][ $key ]['question'] ) . '</li>' . PHP_EOL .
'<li><b>Answer : </b>' . esc_html( $data['entry'][ $key ]['answer'] ) . ' - ❌ Wrong (The right answer is: ' . esc_html( $answer['title'] ) . ')</li>';
$message = str_replace( $old, $new, $message );
}
}
}
}
return $message;
}
add_filter( 'forminator_quiz_mail_admin_message', 'wpmudev_forminator_intercept_quiz_answers', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment