Search code examples
phpwordpressfunctionshortcodenotice

How can i call a plugin shortcode from within a function with theme functions.php file?


I use a plugin (Popup Builder) on my WordPress site. Whenever I create a new popup with the plugin, it creates a shortcode for that popup. I want to call on that shortcode from within the theme functions.php file. But I can't seem to get it to work properly.

The shortcode runs only if conditions are met. And in this example it's when a visitor access the site for the first time. I then check for a certain cookie and if that cookie does not exist, the popup will fire up and force the visitor to choose one option from a list of options, and then the cookie will be set with the correct value, once they do.

However I cant seem to find a solution that fires the popup at all. An I also get this notice: Notice: do_shortcode_tag was called incorrectly. Attempting to parse a shortcode without a valid callback:

function check_for_cookies() { 

    // Check if cookie is already set
    if(isset($_COOKIE['tln_valgt_fylke'])) {

        // Do this if cookie is set 
        $string .= 'Hi, and welcome back!' ; 

        return $string;

    } else { 

        // Do this if the cookie doesn't exist

            $shortcode = do_shortcode("[sg_popup id=163]");

            return $shortcode;
        }   

    } 
    add_action('init', 'check_for_cookies');

What am I doing wrong, and what if this is not a good way of accomplishing what I want, then what is?


Solution

  • This is just a guess

    But, I think its a timing issue. You are using the init action to hook into another plugins shortcodes. It's possible that plugin has not yet registered it's shortcode via add_shortcode or if it has registered it, it may not have "included" the file that defines the callback for it (for whatever reason).

    In fact it seems likely because:

    do_shortcode_tag was called incorrectly. Attempting to parse a shortcode without a valid callback

    This indicates the shortcode was called and no callback existed for it. This is not a problem with your code per say. But it says that the plugin has not yet loaded the file that contains the callback.

    You could test this by hooking into an action that happens later in the execution chain, after you know all plugins have been loaded and initialized. Like even wp_head

    Perhaps you could even get away with changing the priority of the hook:

      add_action('init', 'check_for_cookies', 20); //default is 10
    

    This way it's triggered at the end of init, but even then it may be too soon. The only real way to know is to look at the code for the plugin and find out when it's registering it's "stuff". An easy way to do that is add this code to the plugins shortcode callback:

    try{
       throw new \Exception();
    }catch(\Exception $e){
       die("<pre>{$e->getTraceAsString()}</pre>");
    }
    

    This will throw and then catch an exception, and then output the stacktrace. Which may show you exactly where the shortcode is being setup. You'll have to trigger the callback (obviously) for it to work. You can also use print_r(debug_backtrace()) but it's much harder to read IMO.

    PS I been doing a lot of WP work lately and I just had an issue with action timing ... lol. That was why I thought of it, I spent the last 2 days refactoring code. In my case I an replacing the add/edit/profile parts of the user system on both the front and back end. It's a sort of subuser plugin. And there is a lot of competing actions related to that if you know what I mean...