• Resolved pennymachines

    (@pennymachines)


    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
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Dylan – Harmonic Design

    (@dylanharmonicdesign)

    Here you go 🙂

    // 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)
    {
    $field = array(
    "id" => "hdq_redirect",
    "type" => "text",
    "label" => "Redirect on success",
    "placeholder" => "https://",
    "description" => "This is a custom field to only redirect on a passed quiz"
    );
    array_push($fields["Results"]["children"], $field);
    return $fields;
    }
    add_action('hdq_add_quiz_field', '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 = hdq_get_quiz($quiz_id);
    if (isset($quiz_settings["hdq_redirect"]) && $quiz_settings["hdq_redirect"] != null) {
    $redirect = $quiz_settings["hdq_redirect"];
    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.quiz.quiz_pass_percentage;
    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
    }
    Thread Starter pennymachines

    (@pennymachines)

    Thank you, that is most kind. For some reason the redirect didn’t work (maybe something I missed), so I’ve ended up using the following to achieve the same. This requires a FD_PASS_MARKER variable in the Quiz Settings/Results/Quiz pass content text field, which could be styled invisible, but for my purpose the existing visible “Success!” text did the job.


    // HD Quiz correct answer URL redirect
    function fd_quiz_redirect_watcher() {
    static $printed = false;
    if ($printed) return;
    $printed = true;
    ?>
    <script>
    (function () {
    var FD_PASS_MARKER = 'Success!';
    var FD_FAIL_MARKER = 'Failed!';
    var FD_PASS_URL = 'https://mycustom_URL/'; // set this
    var FD_FAIL_URL = ''; // leave blank for no redirect on fail
    var FD_DELAY = 1000; // milliseconds

    var fdInterval = setInterval(function () {
    var bodyText = document.body.innerText || document.body.textContent;

    if (bodyText.indexOf(FD_PASS_MARKER) !== -1) {
    clearInterval(fdInterval);
    if (FD_PASS_URL) {
    setTimeout(function () { window.location.replace(FD_PASS_URL); }, FD_DELAY);
    }
    } else if (FD_FAIL_URL && bodyText.indexOf(FD_FAIL_MARKER) !== -1) {
    clearInterval(fdInterval);
    setTimeout(function () { window.location.replace(FD_FAIL_URL); }, FD_DELAY);
    }
    }, 400);

    setTimeout(function () { clearInterval(fdInterval); }, 20000); // safety stop
    })();
    </script>
    <?php
    }
    add_action('hdq_after', 'fd_quiz_redirect_watcher');
    Plugin Support Dylan – Harmonic Design

    (@dylanharmonicdesign)

    The code I provided was tested and works.

    • 1. Make sure you have the redirect URL in the new field quiz settings
    • 2. Clear your site caches and browser cache since replacing code won’t clear caches

    Your version has several issues with it and I cannot recommend that you use it on your website.

    If it works for you and you are happy, it’s your site! But I feel that I need to warn you about the following:

    • It is running a function every 400ms – that’s more than twice a second. This is very inefficient.
    • The function is loading the entirety of your page content and storing it as a string. It is then checking this massive string for the substring “Success!”. This is a very bad idea! Not only is is extremely slow, but it can be broken easily. If anywhere else on your page you ever put the word Success!, this function will instantly close. What’s worse is that it is doing this all of this every 400ms!
    • Your function auto stops after 20 seconds from when the page loads. If for whatever reason a user visits the page and takes more than 20 seconds to do whatever they need to do, this function will fail.

    My solution will work 100% of the time, every time, allows you to enable and disable it per quiz, only runs once when it is needed, and actually checks the quiz score instead of parsing your entire page looking for a string.

    Thread Starter pennymachines

    (@pennymachines)

    Thank you again and thanks for the detailed critique of my approach (a reminder of the risks of using AI “assistance”. I’ve reinstated your method, deleted local and server-side cache and tested again. Currently the Quiz redirect URL in my quiz settings is firing after correct and incorrect answers. Perhaps I misunderstood or missed something.

    Plugin Support Dylan – Harmonic Design

    (@dylanharmonicdesign)

    My assumption is that you are using the default redirect field that is built into HD Quiz instead of the new custom field we added with this code. Make sure that original field is empty now as we don’t want the full redirection to happen, just our new one.

    The default field will always redirect, the custom field will only redirect if the quiz passed.

    The correct field is called “Redirect on success” on the Results tab of the quiz edit page.

    If for whatever reason it still doesn’t work, you can post a link to a quiz and I’ll take a look and verify that the everything is added correctly or not. If you want to keep the URL private you can send it to me using the contact form at the bottom of harmonicdesign.ca

    Thread Starter pennymachines

    (@pennymachines)

    What a silly boy I’ve been. I don’t know why I couldn’t see that “Redirect on success” field. Working perfectly now. Thanks once more.

Viewing 6 replies - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.