I am creating a popup plugin in WordPress. In my plugin, a popup opens on every page but I just want to show it on home page.
I know how to get it in PHP but don’t know hot to get the home url in jQuery.
setTimeout(function(){
if($.cookie('SFPopup') == null){
$('#sfp_Modal').modal('show');
}else{
$('#sfp_Modal').modal('hide');
}
}, 3000);
This is how I am showing my popup using a cookie condition but I also want to show it only on home page not inner page.
This is my working code
<?php
function sf_display_popup(){
if (is_front_page() == true) {
?>
<script>
jQuery(document).ready(function($) {
$.cookieBar();
setTimeout(function(){
//var site_url = '<?php echo home_url(); ?>';
if($.cookie('SFPopup') == null || $('body').hasClass('home')){
// alert(site_url);
$('#sfp_Modal').modal('show');
}else{
$('#sfp_Modal').modal('hide');
}
}, 3000);
});
</script>
<?php
}
}
?>
try with checking body for class home. If body have home class it is home page.
I might be completly off, but wouldn’t it be enough to check if
document.location.pathname == "/"
?That way you can check whether the adress is http://www.domain.com (<- would be root) or http://www.domain.com/something.
So if
document.location.pathname == "/"
you can execute your script and add the popup.