0

I am currently developing a plugin that uses shortcodes. On my localhost everything works fine just using the shortcode, for example:

[bbit bandname="Lady Gaga"]

This shows a list of bandsintown.com gigs for the artist, like it's supposed to. Now when I install the plugin on a live website from the WordPress repo and I use the exact same code, the post on the website shows the shortcode, instead of the list of gigs.

More information:

  • I already tried other plugins using shortcodes (like Contact Form 7) and they just work.
  • The localhost WordPress and the live environment use the same theme (a custom child theme for "Responsive".
  • Last version has been pushed to the repo, I checked.
  • When I install the plugin on the localhost (from the repo) it works as well.

What can be the problem here?

[edit] I downloaded the Debug Bar and the Debug Bar Shortcodes. It revealed that there's something wrong with registering the shortcode.

1

So this turned out to be the problem:

After running using WP_DEBUG on the live site it turned out $this was not defined in the following line:

public function __construct(){
    add_action('init', function(){
        add_shortcode('bbit', array($this, 'shortcode_handler'));
    });
}

shortcode_handler is a non-static function here, and this code was all called in a static context. It seems there are some differences in my local XAMPP configuration and the servers configuration. I fixed it by removing the old code and adding the following code outside of the class:

add_action('init', function(){
    add_shortcode('bbit', array('BBIT_ShortCode', 'shortcode_handler'));
});

After that I just needed to make the shortcode_handler method static and there was a fix.

  • 2
    Try removing the quotes around $this. You're passing a string '$this' vs the value of $this. – KenB Feb 22 '15 at 6:43
  • O no that's just a typo in my answer, that wasn't in my original code. – Scuba Kay Feb 22 '15 at 13:33
  • With that out of the way, the real issue if you wanted to get this working as a part of a class is to avoid calling the add_action() the way you are. Create a regular function in your class to add with add_action and you'd be able to have your short code handler inside your class. – KenB Feb 22 '15 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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