OK, so one of the reasons was that the code I have you all those months ago was for the original beta version – the version that first introduced hooks and actions to HD Quiz. However, these hooks need to be called a little bit differently in the live version.
I also updated the JavaScript code to be a bit nicer too ??
Please replace that old code with the following. This has been fully tested and confirmed working in the latest version of HD Quiz (and will work for all future versions too!).
However, I noticed some other discrepancies with your site and HD Quiz. For example, the change I noticed in how the functions get called from the beta to the latest version of HD Quiz would only affect when the function is called – but I don’t see the function on your site anywhere :S
Please replace the old code with this in your functions.php
file and check to see if it works now. If it is not working then it means that there is a different issue such as page caching or maybe a child theme.
function hdq_before_mubasher99($quizID)
{
?>
<script>
function hdq_custom_submit(){
let data = {};
let no_answers = 0;
// mark each question with string on whether answer was right, wrong, or unanswered
jQuery("#hdq_<?php echo $quizID; ?> .hdq_question").each(function(){
let data = "";
if(!this.classList.contains("hdq_question_title") && !this.classList.contains("hdq_results_wrapper")){
let sel = jQuery(this).find(".hdq_correct");
if(sel.length > 0){
data = "Correct answer";
} else {
sel = jQuery(this).find(".hdq_wrong");
if(sel.length > 0){
data = "Incorrect answer";
} else {
data = "No answer selected";
no_answers ++;
}
}
}
jQuery("<h2>" + data +"</h2>").prependTo(this);
});
// hdq_score is array of score (score/total questions)
let c = hdq_score[0];
let t = hdq_score[1];
let p = (parseFloat(c) / parseFloat(t)) * 100;
p = p.toFixed(2);
let d =
<p>Total correct answers: ${c}<br/>
Total Wrong Answers: ${t}<br/>
Total Skipped Questions: ${no_answers}<br/>
Total percentage: ${p}%
</p>`;
let results_section = document.querySelectorAll(“.hdq_share”)[0];
results_section.insertAdjacentHTML(“beforebegin”, d);
return JSON.stringify(data); // expects a json string to be returned
}
</script>
<?php
}
add_action(‘hdq_before’, ‘hdq_before_mubasher99’);
function hdq_submit_mubasher99($quizOptions)
{
array_push($quizOptions->hdq_submit, “hdq_custom_submit”);
return $quizOptions;
}
add_action(‘hdq_submit’, ‘hdq_submit_mubasher99’);`