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 Answer
1

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();
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *