John Davidson

php - How can I set this pop up to only show/fire on the home page of the website?

0 comments
Message:


I have a custom popup here https://odfigroup.com/ (made with php like a custom plugin)


It use to only fire when someone was on the home page and on only a certain number of visits but it does not seem to be working anymore.


So all I want it for this popup to display ONLY on the home page. And to fire every time someone hits these visit counts [1,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45]; //fire popup on page visit number


Here is my current code


<?php

function od_cookie(){ //sets cookies - bound to init
if( !isset( $_COOKIE['odpop'] ) ){ // if cookie is not set set cookie to 1 ie first page load
setcookie('odpop', 1, 0, '/');
}else{
$count = intval($_COOKIE['odpop']) + 1;
setcookie('odpop', $count, 1, 0, '/'); // set cookie to session number
}

}
add_action('init','od_cookie');


function odpopwp_fire(){

$fireOn = [1,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45]; //fire popup on page visit number

if(isset($_COOKIE['odpop'])){ //If cookie is set

if(isset($_GET['odpop'])){ //if form has been submitted - GET REQUEST
include WP_PLUGIN_DIR . '/odonnell-popup/public/templates/success-template.php';

} elseif( in_array( $_COOKIE['odpop'], $fireOn ) == true ){//if cookie is equal to a value in $fireOn

include WP_PLUGIN_DIR . '/odonnell-popup/public/templates/odpopwp-template.php';
}

}else{ //if cookie is not set set the cookie.
include WP_PLUGIN_DIR . '/odonnell-popup/public/templates/odpopwp-template.php';

}

}

add_action('wp_footer', 'odpopwp_fire');

Thanks in Advance!



My Answer:
To make the popup only display on the home page, you can add a conditional check to the `odpopwp_fire` function to only include the popup template if the current page is the home page. You can use the `is_front_page()` function provided by WordPress to check if the current page is the home page.

Here's how you can modify your `odpopwp_fire` function:

php
function odpopwp_fire(){

$fireOn = [1,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45]; //fire popup on page visit number

if(is_front_page()){ // Check if current page is the home page

if(isset($_COOKIE['odpop'])){ //If cookie is set

if(isset($_GET['odpop'])){ //if form has been submitted - GET REQUEST
include WP_PLUGIN_DIR . '/odonnell-popup/public/templates/success-template.php';

} elseif( in_array( $_COOKIE['odpop'], $fireOn ) == true ){//if cookie is equal to a value in $fireOn

include WP_PLUGIN_DIR . '/odonnell-popup/public/templates/odpopwp-template.php';
}

}else{ //if cookie is not set set the cookie.
include WP_PLUGIN_DIR . '/odonnell-popup/public/templates/odpopwp-template.php';

}
}

}

add_action('wp_footer', 'odpopwp_fire');


With this modification, the popup will only display on the home page.

Rate this post

5 of 5 based on 8237 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us