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');
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/