在没有插件的情况下,在WordPress中创建cookie bar同意

发布于 2025-01-27 16:46:06 字数 104 浏览 2 评论 0原文

我在这个领域很新,但是我想为我的WordPress主题创建一个cookie同意栏,但没有使用插件或任何外部帮助,只需纯代码(我使用Bootstrap 5.1和PHP)。我希望我很明确。提前致谢。

I am pretty new in this area but I want to create a cookie consent bar for my wordpress theme but without using plugins or any external help, just pure code(I`m using bootstrap 5.1 and php).I hope I was very explicit.Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找个人就嫁了吧 2025-02-03 16:46:06

一个非常基本的是此代码

function custom_consent()
{
if(!isset($_COOKIE['consent_cookie'])):
?>
<div class="your-bootstrap-classes" id="consent-container">
    <p>We use some cookies for best use experience <a id="custom-accept" href="Javascript:void(0)">Agree</a><a id="custom-close" href="Javascript:void(0)">Close</a></p>
</div>
<?php endif; ?>
<script type="text/javascript">
(function($){

$('#custom-close').on('click', function(){
    $('#consent-container').remove();
});

$('#custom-accept').on('click', function(){

    var date = new Date();
    console.log(date.getTime());
    var expires = "";
    // this is for 1 minute, just adjust the time
    date.setTime(date.getTime() + (1*60*1000));

    expires = "; expires=" + date.toUTCString();
    document.cookie = "consent_cookie=true; "+expires+"; path=/";
    $('#consent-container').remove();
});

})(jQuery);
</script>
<?php
}
add_action('wp_footer', 'custom_consent');

A very basic for that will be this code

function custom_consent()
{
if(!isset($_COOKIE['consent_cookie'])):
?>
<div class="your-bootstrap-classes" id="consent-container">
    <p>We use some cookies for best use experience <a id="custom-accept" href="Javascript:void(0)">Agree</a><a id="custom-close" href="Javascript:void(0)">Close</a></p>
</div>
<?php endif; ?>
<script type="text/javascript">
(function($){

$('#custom-close').on('click', function(){
    $('#consent-container').remove();
});

$('#custom-accept').on('click', function(){

    var date = new Date();
    console.log(date.getTime());
    var expires = "";
    // this is for 1 minute, just adjust the time
    date.setTime(date.getTime() + (1*60*1000));

    expires = "; expires=" + date.toUTCString();
    document.cookie = "consent_cookie=true; "+expires+"; path=/";
    $('#consent-container').remove();
});

})(jQuery);
</script>
<?php
}
add_action('wp_footer', 'custom_consent');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文