URL redirect after correct answers
-
Hi and thanks again for this super plugin and for continuing to develop it.
I still use it as a way to keep Spam and bot accounts from registering on my site. No doubt AI-assisted bots will soon be smart enough to get through (I’ve just installed your Limit Attempts addon so they only get 3 tries).
I have some code you gave me in my child theme functions.php that redirects the browser to my registration form only upon successful answering of all questions (so the user doesn’t have to click the displayed link to it). Unsurprisingly, this is no longer compatible with the plugin. I see there is a built-in ‘Quiz redirect URL’ but this seems to fire upon completion of the quiz regardless of right or wrong answers.
I would most be grateful if you could provide the new hook to update my old code below:
// 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);
}, 1500); // 1500 = 1.5seconds.
}
}
}
</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
}
You must be logged in to reply to this topic.