Creating Your Own Template Variables

There will be times when you will want to create your own template variables. The template variables use a pair of filters to manipulate the text. For text before the quiz has been submitted, we use the “mlw_qmn_template_variable_quiz_page” filter. Examples for this filter include the message before quiz, the message before comments, and the message at the end of the quiz. For text after the quiz has been submitted, we use the “mlw_qmn_template_variable_results_page” filter. Examples for this filter include the user emails, the results pages, and the admin emails.

Both filters pass two parameters: $content and $qmn_quiz_array. $content is the text being filtered and $qmn_quiz_array is an array of almost every value for the quiz at that point. So, lets take a look at what will be in the array for the “mlw_qmn_template_variable_quiz_page” filter:

$qmn_quiz_array["quiz_id"]; //The id of the quiz
$qmn_quiz_array["quiz_name"]; //The name of the quiz
$qmn_quiz_array["quiz_system"]; //The scoring system of the quiz

So, to create a template variable using that filter may look something like below. This adds the %QUIZ_NAME% variable which is replaced with the name of the quiz.

add_filter( 'mlw_qmn_template_variable_quiz_page', 'quiz_name_variable', 10, 2 );
function quiz_name_variable( $content, $qmn_quiz_array )
{
    $content = str_replace( "%QUIZ_NAME%" , $qmn_quiz_array["quiz_name"], $content );
    return $content;
}

Now, lets take a look at the array for the “mlw_qmn_template_variable_results_page” filter:

$qmn_quiz_array["quiz_id"]; //The id of the quiz
$qmn_quiz_array["quiz_name"]; //The name of the quiz
$qmn_quiz_array["quiz_system"]; //The scoring system of the quiz
$qmn_quiz_array["total_points"]; //The total points earned by the user
$qmn_quiz_array["total_score"]; //The score percentage earned by the user
$qmn_quiz_array["total_correct"]; //The number of questions the user answered correctly
$qmn_quiz_array["total_questions"]; //The number of questions the user answered
$qmn_quiz_array["user_name"]; //The name of the user
$qmn_quiz_array["user_business"]; //The business of the user
$qmn_quiz_array["user_email"]; //The email of the user
$qmn_quiz_array["user_phone"]; //The phone number of the user
$qmn_quiz_array["user_id"]; //The user id of the user
$qmn_quiz_array["question_answers_array"]; //The answers/comments given by the user
$qmn_quiz_array["timer"]; //The amount of seconds the user took to complete the quiz
$qmn_quiz_array["comments"]; //The comments provided by the user in the comments area
$qmn_quiz_array["certificate_link"]; //The link to the certificate generated for the user

So, to create a template variable using that filter may look something like below. This adds the %CORRECT_SCORE% variable which is replaced with the score that the user earned.

add_filter( 'mlw_qmn_template_variable_results_page', 'correct_score_variable', 10, 2 );
function correct_score_variable( $content, $qmn_quiz_array )
{
    $content = str_replace( "%CORRECT_SCORE%" , $qmn_quiz_array["total_score"], $content );
    return $content;
}

Lastly, you may want to add your variable to the list of variables. You can do so using the “qmn_template_variable_list” hook. So, for example:

add_action( 'qmn_template_variable_list', 'add_variable_display' );
function add_variable_display()
{
    ?>
        %CURRENT_DATE% – 

<?php }