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/
Related Posts:
- Understanding WordPress child theme custom JS loading
- Redirect to another page using contact form 7? [closed]
- How can I prevent a shortcode div from extending beyond its boundaries?
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- Proper use of Output Buffer
- List of all theme customizer control types?
- How to remove hardcoded characters from playlists?
- function_exists call in function.php
- WordPress Infinite Scroll without using any plugin
- Shortcode putting html such as
- Debugging an error: wp_enqueue_style was called incorrectly
- Conditionally loading JavaScript based on the Advanced Custom Fields in the post
- Calling a method from functions.php on a click of a button
- Hide a menu-item and its submenus and display a ‘Log in’ link if the user is logged out
- How to use WordPress (PHP) functions in AngularJS partials files?
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- Multiple do_shortcode($content) within one shortcode
- Remove css styles from specific page
- Use different javascript files for each page on website
- Custom plugin issue when trying to use the shortcode twice on a page [closed]
- WordPress menu deletes when trying to add a hook
- Click loads template via ajax
- Can I change a variable in a content part while calling it?
- Nested shortCode functions in the functions.php file
- Making Quote Plugin more efficient
- What is the fastest way to load PHP functions that are only used in one theme template?
- How to speed up admin-ajax.php in wordpress
- My Own layout in WooCommerce pages [closed]
- Differences when using the the_time and the_date functions
- Adding tawk.to code just before body tag on functions.php file
- is_page “range” for if statement?
- Can’t load WP function into external function
- Use menu link or onClick to set a variable
- Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
- Showing content from one page on another
- Converting HTML Template to WordPress Theme
- How to remove the cufon script from Dzonia Lite theme [closed]
- dynamic dependent select dropdown
- Ajax load more button for comments wordpress
- Populate editor with some content of a page with a page template
- Displaying recent posts on static page with template-part via shortcode
- Skt full width basic slideshow problem
- How to Reference/Echo Variable from Another PHP Function
- Is the “_s” on this `sprintf(__(‘Page %s’, ‘_s’), max($paged, $page))` just refer to a text domain?
- Noscript not working as it should in wordpress site
- Shortcode with PHP issue “Undefined index”
- WordPress admin-ajax.php
- What exactly do this function declared into functions.php file of a WP theme?
- Display Data in Table from External Database in WP using Shortcodes
- Is There A Way To Make Theme Files Accept Shortcodes?
- How to call multiple functions from multiple files into a WordPress page template [closed]
- Is it possible to define variables in a wordpress shortcode, and then call the shortcode using a specific variable?
- calling a custom field value inside functions.php
- What is the best way to define constant options for a theme?
- List all blogs, but exclude the main site
- wp_nav_menu not working correctly in my underscores theme
- Why i can’t get custom fields value or post ID via Ajax?
- Help Fallback (thumbnail)
- How to return a string that has a jQuery and Ajax inside in a shortcode?
- How to reference PHP in Javascript
- get_page_templates only return templates with “home” in the filename
- Is there a hack for using is_page() within the function.php file?
- Extract and display user infromation on an automatically created page
- Show different website layout if no sidebar added
- Create special button on WP Tiny MCE Posts Editor for Shortcodes
- How can I assign separate stylesheets to different pages?
- Need advice on theme customizer and child themes
- How to make sure relative URL works when site is not on root domain?
- Updating Metadata with Shortcode
- Uncaught TypeError: extract(): Argument #1 ($array) must be of type array, null given
- using shortcodes inside gravity forms
- WordPress User Meta value variable into Google Gauge Chart
- Display logged in user name and lastname on page
- Create shortcode for metabox gallery
- Cannot find the php_ini configuration file to find my error log to see why my code is not working
- Gutenberg block don’t save rich text content
- Having trouble creating two shortcodes, one for logged in user and one for visitors
- Display current user metadata on WordPress page
- Reload page with a different shortcode when a user selects from a dropdown
- How Can I Display the Last Modified Date for User Profiles on WordPress?
- How to call a function from functions.php with ajax?
- Shortcode’s output to use as other shortcode’s parameter
- Custom Post type Ajax search results
- WordPress load javascript file if something… (after the page is loaded)
- Open/closed function [closed]
- Need to Echo A Url path to show on a wordpress page
- Theme editing “post thumbnail” help
- Find the method which AJAX GET calls
- Wrapping shortcode content in a span or link
- Where can I find the declaration of `$_wp_theme_features`?
- Calling PHP function doesn’t work in index.php
- My customizer’s setting doesn’t set to the default and needed to click the control’s “Default” button before it’ll be set
- WordPress get_post_meta issue
- Overwriting a Shortcode
- How to add button to top of theme customizer?
- Whats wrong with my code? Need To add String to shortcode? [closed]
- how can I make content from a plugin hidden when user is logged in? [duplicate]
- A next page function with shortcode?
- Load slideshow.css file only if Slideshow is checked / on
- hiddenQuestions.find is not a function