1

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

1 Answer 1

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.com/the-wordpress-hooks-firing-sequence/

5
  • Thanks for the answer. But the problem is that the popup won't trigger at all. I can't get it to trigger. And I believe I need javascript to make that happen. Do you have any tips for that?
    – re-boot
    Dec 23, 2018 at 19:07
  • I just managed to get it working on my website by changing the last line to add_action('wp_head', 'cookie_checker'); Let me know if that works for you so I can update my answer. Dec 23, 2018 at 19:49
  • By the way, I've set the popup's Event to "On load" prntscr.com/lyq1ye Dec 23, 2018 at 19:56
  • OnLoad, will load the popup whenever the page your on are loaded without the conditions in the IF ELSE statement. So, yes the popup loads, but it loads regardless of the "cookie_checker".
    – re-boot
    Dec 23, 2018 at 22:42
  • Our only check is wether the visitor has a cookie named 'tln_cookie'. If that is true - we don't load the popup shortcode. If it's false - we load the popup shortcode. Even if you set the event to On load, there is no way the popup will show if the shortcode is not printed on the page. In other words, the popup cannot disregard the cookie_checker function. If you see the popup, this means that you don't have the cookie so the cookie_checker function has inserted the shortcode. If the popup doesn't open, this means that you have the cookie. Dec 24, 2018 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.