functions - Trigger popup in a php ifelse statement

admin2025-06-05  3

I am trying to trigger a popup within functions.php if certain conditions are met. The if else statement it self works perfectly, but I can't get the popup to open. I've tried calling a shortcode but that returns nothing.

I use a plugin (Popup Builder) for the popup itself. So the only thing I have access to is the unique class and the shortcode of the created popup. The plugin allows me to trigger the popup by onclick, by CSS, Hover, onload and exit intent. But none of those options are usefull when i trie to trigger it via shortcode directly from the functions.php file. As in, they don't work.

I could really use some directions here, becouse I feel I'm going nowhere. Below is the shortcode code I've tried. Ps: I know this can be done with javascript somehow, but since I don't have the id of the popup and my experience with JS are limited at best, I don't really know in which direction to turn. Hope someone can help me out!

function cookie_checker() { 

// Check if cookie is already set
if(isset($_COOKIE['tln_cookie'])) {

// Do this if cookie is set 

} else { 

// Do this if the cookie doesn't exist 

$shortcode =        do_shortcode("[sg_popup id=1]");

return $shortcode;

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

I am trying to trigger a popup within functions.php if certain conditions are met. The if else statement it self works perfectly, but I can't get the popup to open. I've tried calling a shortcode but that returns nothing.

I use a plugin (Popup Builder) for the popup itself. So the only thing I have access to is the unique class and the shortcode of the created popup. The plugin allows me to trigger the popup by onclick, by CSS, Hover, onload and exit intent. But none of those options are usefull when i trie to trigger it via shortcode directly from the functions.php file. As in, they don't work.

I could really use some directions here, becouse I feel I'm going nowhere. Below is the shortcode code I've tried. Ps: I know this can be done with javascript somehow, but since I don't have the id of the popup and my experience with JS are limited at best, I don't really know in which direction to turn. Hope someone can help me out!

function cookie_checker() { 

// Check if cookie is already set
if(isset($_COOKIE['tln_cookie'])) {

// Do this if cookie is set 

} else { 

// Do this if the cookie doesn't exist 

$shortcode =        do_shortcode("[sg_popup id=1]");

return $shortcode;

}
} 
add_action('init', 'cookie_checker');
Share Improve this question asked Dec 23, 2018 at 2:07 re-bootre-boot 479 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You are creating a cookie_checker function that returns the shortcode.
However, the action you are hooking into (init), is executed too early (before the page content is created).
You could instead try to "echo" the shortcode content in the footer of the page by hooking to wp_print_footer_scripts.
An example I haven't tested, but should work:

function cookie_checker() { 

// Check if cookie is already set
if(isset($_COOKIE['tln_cookie'])) {

// Do this if cookie is set 

} else { 

// Do this if the cookie doesn't exist 

echo do_shortcode("[sg_popup id=1]");

}

}
add_action('wp_print_footer_scripts', 'cookie_checker');

You can learn more about the order in which WordPress fires its actions here: http://rachievee/the-wordpress-hooks-firing-sequence/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749073219a316123.html

最新回复(0)