2

I'm building my first shortcode and right now doesn't do to much, here is my code:

class Shortcodes {
  public static function notifications_shortcode($atts, $content = "") {
    return 'notifications foo foo';
  }
}

And I'm calling this from another class like this:

class Loader extends MvcPluginLoader {
  function activate () {
    add_shortcode('ls_notifications', array('Shortcodes', 'notifications_shortcode'));
  }
}

But for some reason I can't use it in any post with [ls_notifications][/ls_notifications]and no error is showed, the only thing that I can think is that the short code ins't added, but I don't know why.

I know that maybe this is related with this question Add_shortcode as a Class to pass arguments to a function and with this page Shortcode API but it seems that I'm missing something.

  • 1
    When is Loder::activate() called? Looking around it looks like it’s only run on plugin activation? Shortcodes need to registered on every page load. – Jacob Peattie Mar 19 '18 at 0:55
  • Didn't know, thanks @JacobPeattie, is there any way to register when the short code is required? – learnercys Mar 19 '18 at 3:52
  • 1
    Shortcodes are normally registered flat in the functions or plugin file, or maybe in the init hook. Where to do it in the context of the WP MVC framework is something you'll need to consult the developers/docs about. – Jacob Peattie Mar 19 '18 at 3:58
  • I think it’s better to do it flat as you mentioned, to avoid adding a different behavior inside the mvc context. I already did it in that way and it worked – learnercys Mar 19 '18 at 22:16
4

Here's a simple example using your original code that successfully registers the shortcode. You need to instantiate the Loader class, then call its activate() method.

class Shortcodes {
  public static function notifications_shortcode( $atts, $content = "" ) {
    return 'notifications foo foo';
  }
}

class MvcPluginLoader {
  function __construct() {
  }
}

class Loader extends MvcPluginLoader {
    function __construct() {
        parent::__construct();
    }

  function add_shortcodes() {
    add_shortcode( 'ls_notifications', array( 'Shortcodes', 'notifications_shortcode' ) );
  }
}

add_action( 'init', 'LoaderInit' );
function LoaderInit() {
    $my_loader = new Loader();
    $my_loader->add_shortcodes();
}
  • The original code seems to be part of github.com/tombenner/wp-mvc. In it the activate() method is an activation hook, so running it on every page load would be inappropriate. – Jacob Peattie Mar 19 '18 at 1:58
  • 2
    I renamed activate() to add_shortcodes() to better describe its intended functionality here. As we know, init is the recommended hook to add shortcodes on. The original question didn't provide any additional context for the activate() method, so I didn't bother renaming it before. – Dave Romsey Mar 19 '18 at 5:26

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.