简体   繁体   English

WordPress - 自定义 18+ 弹出警报无法正常工作

[英]WordPress - custom 18+ pop-up alert doesn't work properly

I trying to make custom 18+ alert (only on first visit), but it doesn't works.. Basicly in custom html template it worked, but on WordPress doesn't The view on the website And here , how it should looks, but the MUST HAVE thing is that visitor of website must see the website, this is just something like modal..我试图制作自定义 18+ 警报(仅在第一次访问时),但它不起作用.. 基本上在自定义 html 模板中它有效,但在 WordPress 上没有网站上的视图在这里,它应该看起来如何,但必须有的是网站的访问者必须看到该网站,这就像模态一样..

The second MUST HAVE thing is, it should to show only on first visit, and after I click on "POKRAČOVAT" button, this alert should disappear and doesn't show anywhere on the website.第二个必须要做的是,它应该只在第一次访问时显示,当我点击“POKRAČOVAT”按钮后,这个警报应该消失并且不会显示在网站上的任何地方。 I already tried an cookie way, via.我已经尝试了一种 cookie 方式,通过。 Make a splash screen appear only on first visit in Wordpress (the accepted answer) 仅在第一次访问 Wordpress 时出现启动画面(已接受的答案)

And I tried too this way: Display A Popup Only Once Per User (the accepted answer too) but it doesn't works for me.. Let me to show you code below (the second option), in header.php :我也尝试过这种方式: 每个用户仅显示一次弹出窗口(也接受了答案),但它对我不起作用..让我在header.php中向您展示下面的代码(第二个选项):

<script type="text/javascript"> 
        $(document).ready(function() {
            if(localStorage.getItem('age-test') != 'shown'){
                $("#snippet-ageTest-alertbox").delay(2000).fadeIn();
                localStorage.setItem('age-test','shown')
            }

            $('#snippet-ageTest-alertbox-close').click(function(e) // You are clicking the close button
            {
                $('#snippet-ageTest-alertbox').fadeOut(); // Now the pop up is hiden.
            });
            $('#snippet-ageTest-alertbox').click(function(e) 
            {
                $('#snippet-ageTest-alertbox').fadeOut(); 
            });
        }); 
</script>


<div id="snippet-ageTest-alertbox">        
            <div id="age-test" class="main_background" style="display: block;">
                <div class="age-test-square main_background clearfix">
                    <div class="title">
                        <span>    
                            Pokračovaním potvrzuji, že
                            jsem starší 18 let
                        </span>
                    </div>
                    <div>
                        <a class="agree button-conversion" href="#">
                            Pokračovat
                        </a>
                    </div>
                </div>
            </div>

</div>

You need to both set and then check a cookie.您需要设置并检查 cookie。 Since this isn't native to jQuery, you need a function for that.由于这不是 jQuery 所固有的,因此您需要一个 function。 Then like you were trying, you want to check that the cookie is set before you show the popup.然后就像您尝试的那样,您想在显示弹出窗口之前检查 cookie 是否已设置。

An example of javascript cookies can be found here https://www.w3schools.com/js/js_cookies.asp javascript cookies 的示例可以在这里找到https://www.w3schools.com/js/js_cookies.asp

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    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);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

jQuery(function($){
    // define your cookie's name.
    let c = getCookie('age-test');
    // check if the cookie is set and if not show modal.
    if (c !== 'shown'){
        $("#snippet-ageTest-alertbox").delay(2000).fadeIn();
        setCookie('age-test', 'shown', '99'); // set number of days expired.
    }
});

Rather than adding custom codes in your WordPress web app, WordPress offers a tone of plugins that can do the same thing with high accuracy, so I would advise you to use plugins rather than embedding 3rd party code snippets. WordPress 不是在您的 WordPress web 应用程序中添加自定义代码,而是提供了一种可以高精度执行相同操作的插件,因此我建议您使用插件3方嵌入代码片段而不是使用插件。

Here are some plugins you could use这里有一些你可以使用的插件

  1. Age gate - https://wordpress.org/plugins/age-gate/年龄门 - https://wordpress.org/plugins/age-gate/
  2. CPS age verification - https://wordpress.org/plugins/surbma-yes-no-popup/ CPS 年龄验证 - https://wordpress.org/plugins/surbma-yes-no-popup/

read more about them here https://kinsta.com/blog/wordpress-age-verification/在此处阅读有关它们的更多信息https://kinsta.com/blog/wordpress-age-verification/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM