I tried to set cookie on my Wordpress website.
So i write the following code on header.php
if($_REQUEST['my-key'] !==""){
$value=$_REQUEST['my-key'];
setcookie('new_my_code', $value, time() + (86400));
}
But the problem of this is this cookie is only setting up with the corresponding page , not the entire domain .
For example if someone take www.mywebsite.com/about/?my-key=123
then the cookie is set on /about path only . I want to set it on entire pages or entire domain , i tried many things nothing works
Try 1) header.php
if($_REQUEST['my-key'] !==""){
$value=$_REQUEST['my-key'];
setcookie('new_my_code', $value, time() + (86400), '/');
}
Try 2) header.php
if($_REQUEST['my-key'] !==""){
$value=$_REQUEST['my-key'];
setcookie('new_my_code', $value, time() + (86400), '/', '.mywebsite.com');
}
Try 3) header.php
if($_REQUEST['my-key'] !==""){
$value=$_REQUEST['my-key'];
setcookie('new_my_code', $value, time() + (86400), COOKIEPATH, COOKIE_DOMAIN);
}
Try 4) functions.php
add_action( 'init', 'setting_my_first_cookie' );
function setting_my_first_cookie() {
if($_REQUEST['my-key'] !==""){
$value=$_REQUEST['my-key'];
setcookie('new_my_code', $value, time() + (86400), COOKIEPATH, COOKIE_DOMAIN);
}
}
5)
$rp_path = isset( $_SERVER['REQUEST_URI'] ) ? current( explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '';
setcookie('new_my_code', $value, time() + (86400),$rp_path, COOKIE_DOMAIN, is_ssl(), true);
I tired most things that i found on web to make it work . But whatever i am doing cookie is only setting on the same page . So please help
Is there any solution ? Is there any jQuery Solution ?
JS is good for this
document.cookie
is all you need but you can wrap it in a function and execute it
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));//set e
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
example from W3 https://www.w3schools.com/js/js_cookies.asp
then you have to run the function on an event // onpageload
document.addEventListener("DOMContentLoaded", function() {
setCookie("cookieName", "value", 10);
});
you can also try dynamically setting the cookie with getting the relative permalink
function get_relative_permalink( $url ) {
$url = get_permalink();
return str_replace( home_url(), "", $url );
}
if($_REQUEST['my-key'] !==""){
$value=$_REQUEST['my-key'];
$path = get_relative_permalink();
setcookie('new_my_code', $value, time() + (86400),$path, '.mywebsite.com');
}
This is an issue with WordPress , i guess the reason is because the headers are already sent when your script reaches your set-cookies line !
you can check for that using headers_sent() if it return true so setting cookies will not work .
this line :
setcookie('new_my_code', $value, time() + (86400), '/');
must set cookies for root website
put it in functions.php like this
function setting_my_first_cookie() {
if($_REQUEST['my-key'] !==""){
$value=$_REQUEST['my-key'];
setcookie('new_my_code', $value, time() + (86400), '/');
}
}
add_action( 'init', 'setting_my_first_cookie' );
Hope this help .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.