Using add_shortcode() with namespaces in WordPress

Using add_shortcode() with namespaces is a little unintuitive – you need to always pass the fully qualified function name even though you are in a namespaced PHP file, example:

namespace Foo\Bar;

add_shortcode('my_shortcode', 'Foo\Bar\my_shortcode_callback');

/** Shortcode callback function **/
function my_shortcode_callback($atts, $content = '') {
   //Shortcode logic
}

Leave a Reply

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

Markdown is allowed in comments.