auto redirect and one time quiz
-
first, how do i auto-redirect user once they click the submit button or time elaps,e and also how do I make the user take quiz only once, please it’s urgent
-
Neither of these are built-in functions of HD Quiz. You’ll have to add them in yourself.
The good news is that if you are a developer, or hire a developer, both of those are possible to add in by using the custom HD Quiz
hdq_submit
action. This will allow you to run any functions you want once a quiz has been completed.In your case, 1) an auto redirect, and 2) save that the user has taken the quiz (either by setting a cookie, or better yet – if they are logged in, then saving a record to their profile).
However, if you are not willing to hire a developer then I’m afraid there is little more I can do help. The redirection request is an easy one (let me know if you want me to send you some custom code you can add to your theme to allow for this), but the limiting quizzes to only being able to be completed once is a BIG feature.
send me any codes that will help, please
whether the first or second featurejust send me the code for the redirection
when do you want it to redirect? Only if the user passes the quiz, or no matter what?
No matter what, once the time elapse or user click submit, then maybe display a thank you message and redirect
hello I’m still waiting please
Please be patient. I’m doing writing custom code for you on weekend for free.
STEP 1. Make a backup of your theme’s
functions.php
file in case you mess up adding this custom code. It is always recommended to backup this file before making any modifications because it is such an important file.Paste in the following code at the very top after the opening
<?php
// Tell HD Quiz to run a function once the quiz has been completed function hdq_faradaydeman_submit($quizOptions) { array_push($quizOptions->hdq_submit, "hdq_faradaydeman_submit"); return $quizOptions; } add_action('hdq_submit', 'hdq_faradaydeman_submit'); // Add new custom "redirect" field to quiz settings results tab function hdq_faradaydeman_add_custom_fields_to_quizzes($fields) { // slug of the tab you want to add the field to $tab = "results"; // Set the tab (used in case this is a non default tab) if (!isset($fields[$tab]) || !is_array($fields[$tab])) { $fields[$tab] = array(); } // set field settings $field = array(); $field["name"] = "hdq_redirect"; // should be slug format $field["label"] = "Redirect link"; $field["type"] = "text"; $field["placeholder"] = "https://"; array_push($fields[$tab], $field); return $fields; } add_action('hdq_add_quiz_meta', 'hdq_faradaydeman_add_custom_fields_to_quizzes'); // add custom function that will run once the quiz has been completed function hdq_faradaydeman_redirect($quiz_id) { $quiz_settings = get_hdq_quiz($quiz_id); if (isset($quiz_settings["hdq_redirect"]["value"]) && $quiz_settings["hdq_redirect"]["value"] != null) { $redirect = $quiz_settings["hdq_redirect"]["value"]; hdq_faradaydeman_print_redirect($redirect); } ?> <script> function hdq_faradaydeman_submit() { if (typeof hdq_redirect_url != "undefined") { setTimeout(function() { window.location.replace(hdq_redirect_url); }, 3000); // 3000 = 3seconds. } } </script> <?php } add_action('hdq_after', 'hdq_faradaydeman_redirect'); function hdq_faradaydeman_print_redirect($redirect) { ?> <script> let hdq_redirect_url = "<?php echo $redirect; ?>"; </script> <?php }
The above code does the following:
1. Adds a new textbox to the quiz settings results page where you can add in a redirect link.
2. On quiz load, detects if a redirection link exists, if it does exist, then we set up a custom function to run once the quiz has been completed.
3. we run our custom function, which in this case, is to redirect to a new page after 3 seconds.The only thing you might want to modify is the number
3000
in thehdq_faradaydeman_submit
function. This number represents 3seconds (in milliseconds) – the time we wait to redirect. Changing this to0
will make the redirection instant, and changing it to10000
will make it redirect after 10 seconds.thanks very much, really appreciate the support even though it was on a weekend, you really amazing….thanks
but i have added the code nothing is happening
Edit: it’s now working- This reply was modified 4 years ago by faradaydeman.
please one more request please, how do I remove the result score that shows after the quiz is taken….i don’t want my users to know their results till I announce it….thanks
never mind i have been able to remove the result wrapper from the function page… thanks
Hi
Would it be possible to post the code required to auto redirect to a page only if the quiz is completed successfully.@pennymachines the following should work ??
// Tell HD Quiz to run a function once the quiz has been completed function hdq_faradaydeman_submit($quizOptions) { array_push($quizOptions->hdq_submit, "hdq_faradaydeman_submit"); return $quizOptions; } add_action('hdq_submit', 'hdq_faradaydeman_submit'); // Add new custom "redirect" field to quiz settings results tab function hdq_faradaydeman_add_custom_fields_to_quizzes($fields) { // slug of the tab you want to add the field to $tab = "results"; // Set the tab (used in case this is a non default tab) if (!isset($fields[$tab]) || !is_array($fields[$tab])) { $fields[$tab] = array(); } // set field settings $field = array(); $field["name"] = "hdq_redirect"; // should be slug format $field["label"] = "Redirect link"; $field["type"] = "text"; $field["placeholder"] = "https://"; array_push($fields[$tab], $field); return $fields; } add_action('hdq_add_quiz_meta', 'hdq_faradaydeman_add_custom_fields_to_quizzes'); // add custom function that will run once the quiz has been completed function hdq_faradaydeman_redirect($quiz_id) { $quiz_settings = get_hdq_quiz($quiz_id); if (isset($quiz_settings["hdq_redirect"]["value"]) && $quiz_settings["hdq_redirect"]["value"] != null) { $redirect = $quiz_settings["hdq_redirect"]["value"]; hdq_faradaydeman_print_redirect($redirect); } ?> <script> function hdq_faradaydeman_submit() { if (typeof hdq_redirect_url != "undefined") { // figure out if the user passed; let pass_percent = HDQ.VARS.pass_percent; let score = HDQ.VARS.hdq_score[0] / HDQ.VARS.hdq_score[1] * 100; if(score >= pass_percent){ setTimeout(function() { window.location.replace(hdq_redirect_url); }, 3000); // 3000 = 3seconds. } } } </script> <?php } add_action('hdq_after', 'hdq_faradaydeman_redirect'); function hdq_faradaydeman_print_redirect($redirect) { ?> <script> let hdq_redirect_url = "<?php echo $redirect; ?>"; </script> <?php }
Thank you so much. That’s great. Here’s what I’m using this for: HD Quiz kills WP Spam accounts
- The topic ‘auto redirect and one time quiz’ is closed to new replies.