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)
- Calling a function via a shortcode in javascript
- How to manually fix the WordPress gallery code using PHP in functions.php?
- Function to return true if current page has child pages
- Proper use of Output Buffer
- List of all theme customizer control types?
- Need help setting default setting value for radio button in theme customizer
- How to call a PHP function from Javascript in WordPress
- 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
- How to preserve PHP modifications while upgrading WordPress?
- Is it possible to get a theme customizer setting from wp.customize using jquery?
- Debugging an error: wp_enqueue_style was called incorrectly
- Conditionally loading JavaScript based on the Advanced Custom Fields in the post
- How to get the registered sidebar’s name by its id?
- Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
- Dynamically change feature image in customiser
- One button to change all settings in theme customizer?
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Calling a method from functions.php on a click of a button
- Shortcode created to check language not works
- 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”
- Run shortcode at certain resolution
- Design view breaking on Pages
- Add/echo div with Analytics-Code to function in functions.php
- Multiple do_shortcode($content) within one shortcode
- How can I loop into two different DIVS without repeating the DIVs
- Removing unnecessary wordpress files
- Getting text from custom field from customizer
- Time Delay a URL Redirect for Specific Page
- Dynamically switch file in get_template_directory_uri() | Function [closed]
- Updating Metadata with Shortcode
- Need help for some PHP code
- 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
- How to extend SelectControl with data from my theme
- How to change basename url for wp-admin?
- 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
- Adding HTML Code to Replace Text in PHP
- how to replace h1 entry title with h2 in category pages only
- Display Visual Composer shortcode if a post belongs in specific categories
- Why is the current page loaded in the pop-up window and not the specified one?
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Not able to remove caption shortcode from the content
- Block error message in foreach loop when looping through ACF field
- Gutenberg block don’t save rich text content
- Problem with displaying CSS Stylesheets – Am I adding them correctly in my wordpress child theme?
- require get_template_directory() . ‘path/to-my/file.php’ BREAKS customize > themes functionality
- PHP multiple forms, same page, isset($_POST[]) not working?
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- shortcodes, custom php and their errors
- Trying to add some custom text into WordPress Post title via function.php
- Display a custom name when the user has no name settle in his account
- Having trouble creating two shortcodes, one for logged in user and one for visitors
- Update $wpdb query with AJAX
- Using Javascript On Page – Header and Footer Now Missing
- register dependency css and js inside a plugin class
- Adding extra data to shortcode attributes and pass it to JS with wp_localize_script
- 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 output values from a loop into a javascript array
- Collapsible menu on post sidebar only expands and does not collapse
- Replacing entire tag with shortcode – JavaScript [duplicate]
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- How do I make comment-reply-button with that takes to comment reply form on click
- Menu to the right of screen on desktop using Bootstrap 4
- Offset with ajax load more posts duplicates
- custom post type column countdown
- Get posts by id using shortcode
- 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)
- Automatically refresh page if widget is added to page?
- wordpress all post filter by year
- Open/closed function [closed]
- How to send Ajax Call from frontend without using wp_localize_script in Theme
- Need to Echo A Url path to show on a wordpress page
- Theme editing “post thumbnail” help
- Force ‘permanent’ post cache of shortcode results
- How can i iterate through this shortcode array?
- Find the method which AJAX GET calls
- Display text of price (minus 20%) on every product page in a sentence.
- Wrapping shortcode content in a span or link
- Need help with PHP functions
- Where can I find the declaration of `$_wp_theme_features`?
- Calling PHP function doesn’t work in index.php
- Extend the WP_Customize_Image_Control class to change its non-frame $button_labels