jayant rawat
jayant rawat

Reputation: 305

Want to load popup on homepage only using jQuery in wordpress

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
    }
}
?>

Upvotes: 1

Views: 2726

Answers (2)

noa-dev
noa-dev

Reputation: 3641

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 www.domain.com (<- would be root) or www.domain.com/something.

So if document.location.pathname == "/" you can execute your script and add the popup.

Upvotes: 0

softbrewery
softbrewery

Reputation: 501

try with checking body for class home. If body have home class it is home page.

if ($('body').hasClass('home')) { // show pop up }

Upvotes: 2

Related Questions