Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oropesa/e1eae4e222bc41ecc8e9 to your computer and use it in GitHub Desktop.
Save oropesa/e1eae4e222bc41ecc8e9 to your computer and use it in GitHub Desktop.
Wordpress Cookies Managing. Is not all the code, but It's the first template.
(function(){
var //cookie
, $btnCookie = $('#btn-acceptCookies')
, $divCookie = $('div.cookiesAlert')
, theCookieKey = 'THE_SITE_NAME_cookie_policy'
, theCookieVal = 'accepted'
, theCookieDays = 15
//cookie erasing
, $btnDeleteCookies = $('#btn-deleteAllCookies')
, cookieDeleteText = 'Our cookies have been removed from your device successfully. '
+ '<a id="deleteAllCookiesConfirm" class="btn btn-danger btn-cookies" href="#deleteAllCookiesConfirm">OK</a>'
//cookie functions
,setCookie = function(key, value, days) {
var expires = ''
, date = new Date()
;
if (days) {
date.setTime( date.getTime()+(days*24*60*60*1000) );
expires = "; expires=" + date.toGMTString();
}
document.cookie = key + "=" + value + expires + "; path=/";
}
, eraseCookie = function(key) {
setCookie(key, "", -1);
}
, eraseAllCookies = function() {
var cookies = document.cookie.split(';')
, cookiesLength = cookies.length
, i = 0
;
for(;i < cookiesLength; i++) {
eraseCookie(cookies[i]);
}
}
, getCookie = function(key) {
var keyEQ = key + "="
, cookies = document.cookie.split(';')
, cookie
, i = 0
;
for(;i < cookies.length; i++) {
cookie = cookies[i];
while (cookie.charAt(0) == ' ')
cookie = cookie.substring(1, cookie.length);
if (cookie.indexOf(keyEQ) == 0)
return cookie.substring(keyEQ.length, cookie.length);
}
return null;
}
;
//cookies
if($btnCookie.length) {
$btnCookie.on('click', function(e) {
e.preventDefault();
setCookie(theCookieKey, theCookieVal, theCookieDays);
$divCookie.addClass(theCookieVal);
});
}
if($btnDeleteCookies.length) {
$btnDeleteCookies
.popover({
'content' : cookieDeleteText
, 'html' : true
})
.on('click', function(e) {
e.preventDefault();
eraseAllCookies();
});
$btnDeleteCookies.on('shown.bs.popover', function () {
$('#deleteAllCookiesConfirm').on('click', function(e) {
e.preventDefault();
$btnDeleteCookies.popover('hide');
});
})
}
})(jQuery);
//COOKIE MESSAGE
.cookiesAlert {
background: @darkgray;
color: white;
font-size: 1.3rem;
overflow: hidden;
text-align: center;
&.accepted {
.container {
margin-top: -30%;
}
}
.container {
.transition(all ease 3s);
position: relative;
overflow: hidden;
padding: 1.5rem;
a {
color: @white;
text-decoration: underline;
white-space: nowrap;
&.btn-cookie {
margin: .7rem;
padding: .3rem .8rem;
background: white;
color: @darkgray;
text-decoration: none;
border-radius: 5px;
border-bottom: 2px solid darken(@white, 25%);
}
}
p {
margin: 0;
}
}
}
//Button Delet Cookie
.btn-cookies {
padding: 0 .5rem;
font-size: 1.2rem;
position: relative;
display: inline-block;
top: -2px;
margin-left: .2rem;
}
.popover {
font-size: 1.3rem;
border-color: @brand-danger;
color: @brand-danger;
&.right > .arrow {
border-right-color: @brand-danger;
}
}
<?php
/**
* Hook this on the top of the body
*/
function cookie_message() {
if( isset($_COOKIE['THE_SITE_NAME_cookie_policy']) ){
$theCookieDays = 15; //expire 15 days
setcookie('THE_SITE_NAME_cookie_policy', 'accepted', time()+($theCookieDays*3600*24), '/');
return;
}
$cookieURL = 'http://THE_SITE_NAME_URL/COOKIES_PAGE';
?>
<div class="cookiesAlert">
<div class="container">
<div class="col-xs-12 col-sm-12">
<p>In <?php bloginfo('name') ?> We use proprietary and third party cookies. We consider that if you continue to browse accepts use. <a href="<?php echo $cookieURL ?>">Learn more</a><a class="btn-acceptCookies" href="#aceptar-cookies">Ok</a></p>
</div>
</div>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment