• Resolved DigitalFormula

    (@digitalformula)


    Hi all,

    All my theme and plugin classes are part of my custom namespace, except for the shortcodes.

    The classes have standard PHP __construct() methods that call methods like register_nav_menu(), for example. They work fine.

    However, when I modify my shortcode file so that it’s part of my namespace and call add_shortcode() in the constructor, the shortcodes no longer work – they just display the [shortcode params=””] markup. If the file isn’t part of the namespace and I call add_shortcode() at the end of the file, they work fine.

    Here’s one of the add_shortcode() calls:

    add_shortcode( 'social', 'social_shortcode');

    The function’s code is like this (with all the main parts removed as they aren’t relevant):

    function social_shortcode( $atts )
    {
       /* do shortcode stuff here */
    }

    Is there a special way of calling add_shortcode() when the file is part of a namespace? If not, what am I doing wrong?

    I’ve selected 3.7.1 as the WP version, although I’m using 3.8-beta1. The results are the same if I test the code on a production 3.7.1 site, though.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter DigitalFormula

    (@digitalformula)

    This is now resolved. The solution is to use the following when calling add_shortcode():

    add_shortcode( 'social', array( &$this, 'social_shortcode' ) );

    HTH someone.

    Just a heads up, you no longer have to pass $this by reference (the &$this) as of PHP 5. Feel free to save a keystroke and just use $this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to add shortcodes to PHP namespace’ is closed to new replies.