• Resolved faradaydeman

    (@faradaydeman)


    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

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Harmonic Design

    (@harmonic_design)

    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.

    Thread Starter faradaydeman

    (@faradaydeman)

    send me any codes that will help, please
    whether the first or second feature

    Thread Starter faradaydeman

    (@faradaydeman)

    just send me the code for the redirection

    Plugin Author Harmonic Design

    (@harmonic_design)

    when do you want it to redirect? Only if the user passes the quiz, or no matter what?

    Thread Starter faradaydeman

    (@faradaydeman)

    No matter what, once the time elapse or user click submit, then maybe display a thank you message and redirect

    Thread Starter faradaydeman

    (@faradaydeman)

    hello I’m still waiting please

    Plugin Author Harmonic Design

    (@harmonic_design)

    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 the hdq_faradaydeman_submit function. This number represents 3seconds (in milliseconds) – the time we wait to redirect. Changing this to 0 will make the redirection instant, and changing it to 10000 will make it redirect after 10 seconds.

    Thread Starter faradaydeman

    (@faradaydeman)

    thanks very much, really appreciate the support even though it was on a weekend, you really amazing….thanks

    Thread Starter faradaydeman

    (@faradaydeman)

    but i have added the code nothing is happening
    Edit: it’s now working

    Thread Starter faradaydeman

    (@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

    Thread Starter faradaydeman

    (@faradaydeman)

    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.

    Plugin Author Harmonic Design

    (@harmonic_design)

    @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

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘auto redirect and one time quiz’ is closed to new replies.