I want to show a popup to the first time visitor of a Wordpress site. I tried to check the visit state using $_SESSION
. Something like this in the footer.php:
<?php
if(!isset($_SESSION['pxpop']))
$_SESSION['pxpop']= true;
if(($_SESSION['pxpop']) && (!is_user_logged_in()))
{
?>
<div class="open_initpop">
<?php if(is_active_sidebar('msg-pop')): ?>
<?php dynamic_sidebar('msg-pop'); ?>
<?php endif; ?>
</div>
<?php
$_SESSION['pxpop']= false;
}
?>
with session_start();
in the init
hook of the functions.php.
But this is not working. $_SESSION['pxpop']
remains true
on each page load. So the popup opens on each page.
With a little r&d I have found that due to some 'statelessness' issue wordpress does not use sessions. From the site health section, it also says:
"PHP sessions created by a session_start() function call may interfere with REST API and loopback requests. An active session should be closed by session_write_close() before making any HTTP requests."
Then I tried implementing $_COOKIE
too (in the init
hook) as:
<?php
function pop_event()
{
if(!isset($_COOKIE['pxpop']))
{
setcookie('pxpop', true, 0);
//$_COOKIE['pxpop']= true;
}
if(($_COOKIE['pxpop']) && (!is_user_logged_in()))
{
?>
<div class="open_initpop">
<?php if(is_active_sidebar('msg-pop')): ?>
<?php dynamic_sidebar('msg-pop'); ?>
<?php endif; ?>
</div>
<?php
//unset($_COOKIE['pxpop']);
//$_COOKIE['pxpop']= false;
setcookie('pxpop', false, 0);
}
}
?>
But this is not working too...
What is wrong with my approach? What is the correct way to carry forward a value in Wordpress like PHP session without actually using it? Or using $_SESSION is the only resort?
I want to show a popup to the first time visitor of a Wordpress site. I tried to check the visit state using $_SESSION
. Something like this in the footer.php:
<?php
if(!isset($_SESSION['pxpop']))
$_SESSION['pxpop']= true;
if(($_SESSION['pxpop']) && (!is_user_logged_in()))
{
?>
<div class="open_initpop">
<?php if(is_active_sidebar('msg-pop')): ?>
<?php dynamic_sidebar('msg-pop'); ?>
<?php endif; ?>
</div>
<?php
$_SESSION['pxpop']= false;
}
?>
with session_start();
in the init
hook of the functions.php.
But this is not working. $_SESSION['pxpop']
remains true
on each page load. So the popup opens on each page.
With a little r&d I have found that due to some 'statelessness' issue wordpress does not use sessions. From the site health section, it also says:
"PHP sessions created by a session_start() function call may interfere with REST API and loopback requests. An active session should be closed by session_write_close() before making any HTTP requests."
Then I tried implementing $_COOKIE
too (in the init
hook) as:
<?php
function pop_event()
{
if(!isset($_COOKIE['pxpop']))
{
setcookie('pxpop', true, 0);
//$_COOKIE['pxpop']= true;
}
if(($_COOKIE['pxpop']) && (!is_user_logged_in()))
{
?>
<div class="open_initpop">
<?php if(is_active_sidebar('msg-pop')): ?>
<?php dynamic_sidebar('msg-pop'); ?>
<?php endif; ?>
</div>
<?php
//unset($_COOKIE['pxpop']);
//$_COOKIE['pxpop']= false;
setcookie('pxpop', false, 0);
}
}
?>
But this is not working too...
What is wrong with my approach? What is the correct way to carry forward a value in Wordpress like PHP session without actually using it? Or using $_SESSION is the only resort?
You can just use a cookie, like you've attempted. The problem is that your logic is continuously adding the cookie. This is because setcookie('pxpop', false, 0);
is removing the cookie from the user, so you end up in a loop. This is your code's logic:
IF user does not have a cookie
give the user a cookie
END IF
IF user has a cookie
display popup
take the cookie
END IF
So you can see why nothing will change on each load, because the user never visits the page with a cookie.
What you want to do is:
IF user does not have cookie
display popup
give user the cookie
END IF
Also, your cookie is set to expire when the browser session ends, so it wouldn't last long regardless.
Addressing both issues would look like this:
<?php
function pop_event() {
if ( ! isset( $_COOKIE['pxpop'] ) ) {
if ( is_active_sidebar( 'msg-pop' ) ) {
?>
<div class="open_initpop">
<?php dynamic_sidebar('msg-pop'); ?>
</div>
<?php
setcookie( 'pxpop', true, YEAR_IN_SECONDS );
}
}
}
Keep in mind that this solution is not going to be compatible with many caching solutions. My recommendation would be to implement this entirely in the client, since you're presumably going to be using JavaScript already for the popup.
Thanks for the answer given by @Jacob Peattie. I have implemented it in a JavaScript way. Here is the code:
function pop_message()
{
var popshow= sessionStorage.getItem("pxpop");
if((popshow==null) || (popshow==''))
popshow=false;
if(($(".open_initpop").length >0) && (popshow==false))
{
$(".open_initpop").dialog({'autoOpen': true, 'modal':true, 'width':500, 'dialogClass': 'initpop_dlg', 'hide': {'effect': "clip", 'duration': 250}, 'buttons':{'close': function(){
$(this).dialog( "close" );}}});
sessionStorage.setItem("pxpop", true);
}
}
Just call the function pop_message()
in $(document).ready