codex – How do I define and register a shortcode function in a namespaced functions.php file?

Question

Thanks to Stanislav Khromov (also active on WPSE!), I learned any Callable referenced in add_shortcode() must be fully qualified, even if it is defined in the same file you’re referencing it.

<?php
/**
 * theme's functions.php
 */

namespace MyNamespace;

//[foobar]
function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'MyNamespacefoobar_func' );

You may omit the leading backslash, if you prefer: add_shortcode( 'foobar', 'MyNamespacefoobar_func' );

0
Jacob Ford 3 weeks 2021-11-07T17:36:46-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse