Add_shortcode not working (and how to fix it)

If you wonder why your shortcode does not work as expected in WordPress (or does not work at all), it might be because you did not registered it correctly.

Try this one, instead (I use “gallery” as example – but it could be anything).

remove_shortcode('gallery', 'gallery_shortcode');
add_action('init', 'add_my_gallery_shortcode’, 99);
function add_my_gallery_shortcode() {
  add_shortcode( 'gallery', ‘my_gallery_shortcode’);
}
function my_gallery_shortcode( $attr ) {
  // the new gallery logic here
}

Apparently, shortcodes need to be registered at the “init” level. Otherwise WordPress does not receive them correctly, and does not execute them.

The “99” is the priority. Higher the priority, later the instruction will be executed.

Shortcodes do not need to have a high priority. In fact for the most you may want them to be registered at the very end.

Hopefully this will help. Feel free to add any comment or question on the topic.

Further resources