admin管理员组文章数量:1130349
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
1 Answer
Reset to default 1You 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/
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
1 Answer
Reset to default 1You 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/
本文标签: functionsTrigger popup in a php ifelse statement
版权声明:本文标题:functions - Trigger popup in a php ifelse statement 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749073194a2311899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论