Support » Plugin: Age Gate » Rechallenge

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Phil

    (@philsbury)

    Hi @udvi86,

    What are you trying to do? Lock people out for longer/shorter than the current session?

    Thanks
    Phil

    Thread Starter UdVi86

    (@udvi86)

    Hi!

    I would like set 2 hours. So shorter than the current session.

    Thanks
    Attila

    Plugin Author Phil

    (@philsbury)

    Thanks @udvi86,

    One more thing, are you using JS or PHP mode?

    Thanks
    Phil

    Thread Starter UdVi86

    (@udvi86)

    I’m currently using PHP Mode, but later there will be a cache plugin on the site. So I would be interested ín both versions.

    Thanks
    Attila

    Plugin Author Phil

    (@philsbury)

    Ok cool, think a JS approach should cover both. Give me a few minutes and I’ll come up with a solution.

    Thanks
    Phil

    Plugin Author Phil

    (@philsbury)

    Hi @udvi86,

    Here’s what I’ve come up with, this can go in your functions.php, essentially we look and see if the failed cookie exists and change it if it does:

    
    // add an action to put some JS in the footer
    add_action('wp_footer', 'age_gate_cookie_length');
    
    function age_gate_cookie_length()
    {
        ?>
        <script>
            (function(){
                // function to get the cookie so we can check if it's set
                function getCookie(name) {
                    var nameEQ = name + "=";
                    var ca = document.cookie.split(';');
                    for(var i=0;i < ca.length;i++) {
                        var c = ca[i];
                        while (c.charAt(0)==' ') c = c.substring(1,c.length);
                        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
                    }
                    return null;
                }
    
                // function to set (or essentially update) the cookie
                function setCookie(name, value, time) {
                    time = time || 2;
                    var date = new Date();
                    date.setTime(date.getTime() + (time*60*60*1000));
                    expires = "; expires=" + date.toUTCString();
                    document.cookie = name + "=" + value + expires + ";path=/";
    
                }
    
                /**
                 * 1 is the default value set by the plugin, so if
                 * it is set and equals 1, we change the value and
                 * also set the length to what we need.
                 * Now the value is 2, it won't do this again
                 */
                if (parseInt(getCookie('age_gate_failed')) === 1) {
                    setCookie('age_gate_failed', 2, 2);
                }
            })();
        </script>
    
        <?php
    }
    

    That’s a fair bit of code for the footer, so here’s a version of the function with it minified, just pick the above or below, which ever you want:

    add_action('wp_footer', 'age_gate_cookie_length');
    
    function age_gate_cookie_length()
    {
        ?>
        <script>
            !function(){1===parseInt(function(e){for(var t=e+"=",n=document.cookie.split(";"),i=0;i<n.length;i++){for(var r=n[i];" "==r.charAt(0);)r=r.substring(1,r.length);if(0==r.indexOf(t))return r.substring(t.length,r.length)}return null}("age_gate_failed"))&&function(e,t,n){n=n||2;var i=new Date;i.setTime(i.getTime()+60*n*60*1e3),expires="; expires="+i.toUTCString(),document.cookie=e+"="+t+expires+";path=/"}("age_gate_failed",2,2)}();
        </script>
    
        <?php
    }
    

    Thanks
    Phil

    Thread Starter UdVi86

    (@udvi86)

    Thank you very much, Phil. It works.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.