在没有插件的情况下在 wordpress 中创建一个 cookie 栏同意只是纯代码
Create a cookie bar consent in wordpress without plugins just pure code
我在这方面很新,但我想为我的 wordpress 主题创建一个 cookie 同意栏,但不使用插件或任何外部帮助,只是纯代码(我正在使用 bootstrap 5.1 和 php).希望我提前很explicit.Thanks.
一个非常基本的代码就是这个
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');
我在这方面很新,但我想为我的 wordpress 主题创建一个 cookie 同意栏,但不使用插件或任何外部帮助,只是纯代码(我正在使用 bootstrap 5.1 和 php).希望我提前很explicit.Thanks.
一个非常基本的代码就是这个
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');