I am creating a new plugin and have decided to use WordPress Plugin Boilerplate.

However, I'm not sure where the correct place is to call WP's add_shortcode. I'm stuck on two counts:

  1. add_shortcode doesn't usually have a hook - so I doubt I would call it via my define_admin_hooks or define_public_hooks functions.
  2. I'm not sure how I would specify the callback function for add_shortcode now that everything is object orientated.

So where is the best place for me to call add_shortcode? A short example/snippet would be much appreciated.

  • 3
    there is no canonical WordPress based answer to this, and no "correct" or "best" place as far as WordPress plugins go. You should ask the boilerplate people where they intended it to go. Remember, the boilerplate is not official, 3rd party, and the opinion of a single person that got popular. Other boilerplates exist, and you can create a plugin with no boilerplate at all
    – Tom J Nowell
    Apr 20, 2022 at 22:31

1 Answer 1


Add the below function in /includes/class-plugin-loader.php :

public function add_shortcode( $tag, $component, $callback, $priority = 10, $accepted_args = 2 ) {
        $this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback, $priority, $accepted_args );

You can then define your shortcode in /includes/class-plugin.php within this function:

 * Register all of the hooks related to the public-facing functionality
 * of the plugin.
 * @since    1.0.0
 * @access   private
private function define_public_hooks() {

    $plugin_public = new Plugin_Public( $this->get_plugin_name(), $this->get_version() );

    $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
    $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );

    $this->loader->add_shortcode( 'YOUR-SHORTCODE-NAME', $plugin_public, 'YOUR_CALLBACK_FUNCTION' );


Finally add your callback function in the /public/class-plugin-public.php like this:

public function YOUR_CALLBACK_FUNCTION( $atts ){ 
    // do your shortcode stuff

Change plugin in the filenames above with your own plugin name.

  • Thank you, this looks like the perfect answer for me, but there is no add_shortcode function in my /includes/class-plugin-loader.php file. Maybe the boilerplate generator I used - wppb.me - is using an out-of-date version. Which version of the boilerplate code are you referring to? Apr 20, 2022 at 23:38
  • Just found the solution here: stackoverflow.com/a/53191594/1617737 . Apr 21, 2022 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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