0

I am using this code, but nothing is being shown on the front page. It seems the shortcode is registered correctly though.

function choose_ashtopbar() {
?><img src="/images/flag_globe2.png" class="ttupmg" /><div class="dmzeus"><ul><li class="f-selection">London<ul><li>New York</li><li>Paris</li><li>Milan</li></ul></li></ul></div><?php
}

function register_ashcodes(){
add_shortcode('ashtopbar', 'choose_ashtopbar');
}

add_action( 'init', 'register_ashcodes');
  • You're missing the return part of the shortcode's callback. – birgire Oct 5 '15 at 13:34
  • ok but how do I add that without escaping the html code ? – Joanna Mikalai Oct 5 '15 at 13:36
  • just define a string variable and return it. – birgire Oct 5 '15 at 13:56
2

As birgire mentioned in the comments, "you're missing the return part" to which you replied "how do I add that without escaping the HTML code?"

EDIT: Just a clarification. Your code should have worked, as pointed out by dswebsme. I just tested with and without a return, and both work (although returning would be preferred). The next question would be: are you putting your shortcode tag properly in to your content? e.g. [ashtopbar/] ? and it's not misspelled, right?

Leaving the section about heredoc anyway:

In PHP, you have a number of ways to get complex data (including HTML) in to a string variable. In your case, I would use PHP's heredoc syntax, as you can put virtually anything inside of it and not have to escape the syntax:

<?php

function choose_ashtopbar() {
    $html = <<<HTML
<img src="/images/flag_globe2.png" class="ttupmg" />
<div class="dmzeus">
    <ul>
        <li class="f-selection">London
            <ul>
                <li>New York</li>
                <li>Paris</li>
                <li>Milan</li>
            </ul>
        </li>
    </ul>
</div>
HTML;

    return $html;
}

function register_ashcodes(){
    add_shortcode('ashtopbar', 'choose_ashtopbar');
}

# I'm guessing init has already run by this point
# add_action( 'init', 'register_ashcodes');
# Use a different hook or just register your code
register_ashcodes();

Essentially, your original code was just echoing the HTML when add_shortcode ran the choose_ashtopbar function, which is probably not what you wanted.

  • ok still not showing anything with this code. I am placing it in the functions.php of my child theme. I know the code runs because otherwise it would just display [ashtopbar]. I am calling this like <?php echo do_shortcode("[ashtopbar]"); ?> – Joanna Mikalai Oct 5 '15 at 14:10
  • Ok, so a little of each of these answers contains your final solution: the init hook. You're registering your shortcode on init, but my guess is that init has already happened by this point. Try replacing the add_action line with a direct call to register_ashcodes() instead. – phatskat Oct 5 '15 at 14:14
  • ok sorry i had to disable some other code in my theme for it to work.. phewww – Joanna Mikalai Oct 5 '15 at 14:18
  • Please note though phatskat that your example is right and your clarification is wrong! Shortcodes ought to return their output not echo it as birgire said otherwise it will just appear randomly in the output... – bonger Oct 5 '15 at 14:23
  • 1
    Thanks for the nod @phatskat. You win this round =] Upvoted! – dswebsme Oct 5 '15 at 14:26
4

Why are you using all that. Can't you simply define shortcode with add_shortcode? Here is the function.

Also yes, you are missing the return in your function. Finished code here.

function choose_ashtopbar( $atts, $content ) {
  return '<img src="/images/flag_globe2.png" class="ttupmg" /><div class="dmzeus"><ul><li class="f-selection">London<ul><li>New York</li><li>Paris</li><li>Milan</li></ul></li></ul></div>';
}
add_shortcode( 'ashtopbar', 'choose_ashtopbar' );
1

Your code works as expected. Two things you need to check:

1) In order for shortcodes to render as expected you need to use the_content() within the loop OR wrap your content in apply_filters() as shown here:

echo apply_filters('the_content', $my_content);

The apply_filters() helper tells WordPress to parse the content before it's rendered, resolving things like shortcodes, line breaks, etc. Within the loop, the_content() handles the call to apply_filters() automatically.

2) Be sure you clear any server side or local cache that may be in place while debugging to ensure you can see your changes as you make them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.