• Resolved bdew

    (@bdew)


    In my twentyseventeen-child I have functions.php. This contains:

    <?php
    function shortcode_hdid() {
    	return 'Hello world';
    }
    function add_ggc_shortcodes() {
        add_shortcode('hdid','shortcode_hdid');
    }
    add_action('init', 'add_ggc_shortcodes', 99); 
    ?>

    … and in a page I insert [hdid] but Hello world does not appear when viewing the page.

    This is driving me mad and any help would be most appreciated.

    • This topic was modified 8 years ago by bdew.
    • This topic was modified 8 years ago by bdew.
Viewing 6 replies - 1 through 6 (of 6 total)
  • catacaustic

    (@catacaustic)

    You don’t need to use the ‘init’ action. Just call add_shortcode() directly. That makes me think that maybe calling it using the ‘init’ action is too late (which doesn’t seem right but is the only thing that’s wrong with the code really).

    Moderator bcworkz

    (@bcworkz)

    It shouldn’t be too late no matter when it’s added as long as it’s before the “the_content” filter fires. I tried you sample code in my own installation and it works as expected. Apparently there is a name collision of some sort or other conflict. If you reduce your installation to no plugins and the twentysixteen theme, your code copied into twentysixteen’s functions.php should work, init hook or not.

    Thread Starter bdew

    (@bdew)

    Thank you both for your prompt replies and I tried both suggestions. Initially without success.

    In desperation, I started a brand new page with just [hdid] and it worked! Worked with or without ‘init’ with or without that name [hdid].

    So the problem is with that particular page. So I tried a ‘trusted’ shortcode and that failed too. This issue I can workaround or ignore!

    Thank you both again, topic resolved.

    Moderator bcworkz

    (@bcworkz)

    I’m glad you found a workaround. Great inspiration to try a new page! I’m guessing this initial page uses a special template besides the usual default page.php and outputs content through some other process than calling the_content().

    The shortcode processing script is hooked into “the_content” filter, so output by other means bypasses this filter and thus shortcodes are not processed. If one absolutely needed shortcodes to work with this template, it would need modification to ensure content is passed through do_shortcode() before it is output.

    Thread Starter bdew

    (@bdew)

    Nothing special about the page where shortcodes fail. Default page template – completely “bog standard” (if you’re British you’ll understand this expression, otherwise I leave you to guess!)

    It was a test page where I had been experimenting with codes like & 1004 ; (ie &1004;) and I discovered that Visual mode messed these up, so there was some to-ing and fro-ing between Visual and Text mode. I can only assume Visual mode left something bad and invisible on the page. I don’t trust Visual mode and I never use it.

    I copy/pasted the text from the bad page to the new page and everything worked ok and I deleted the bad page. (If you want me to email it to you you’ll have to tell me where I can find the trash and how do I grab it.).

    Moderator bcworkz

    (@bcworkz)

    Heh, let’s leave it and let it be a mystery ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘add_shortcode not working …’ is closed to new replies.