1

Have been looking at various tutorials and answers here and elsewhere and just can't get this to work. Just trying to display a simple output from a shortcode as below, but it is just outputting the actual shortcode tag to the page instead of the content from the shortcode

Functions.php

<?php 
function welcome_roundels_func(){
    return "testing";
}
add_shortcode( 'welcome_roundels', 'welcome_roundels_func' );
?>

add_action( 'init', 'register_shortcodes');

Usage

[welcome_roundels]

Is there anything else I have to do to enable shortcodes, or should the above work?

Edit I am also using the below code to output the content

$welcomePage = get_post( 5 ); 
echo $welcomePage -> post_content;
4
  • Works for me. What is the point of add_action( 'init', 'register_shortcodes');?
    – s_ha_dum
    Commented Feb 11, 2014 at 15:03
  • @s_ha_dum - Looking at various websites, people suggested adding that to 'enable' the shortcodes - didn't work for me though!
    – wickywills
    Commented Feb 11, 2014 at 15:04
  • @s_ha_dum - Just added my output code to my question if that helps?
    – wickywills
    Commented Feb 11, 2014 at 15:09
  • 1
    You don't need to "enable" shortcodes. That is nonsense. They are built into the WordPress Core and have been for a long time.
    – s_ha_dum
    Commented Feb 11, 2014 at 15:10

1 Answer 1

4

Your problem is here:

$welcomePage = get_post( 5 ); 
echo $welcomePage -> post_content;

Shortcodes are processed on display by via a filter on the_content which is called by the function the_content(). You've bypassed that functionality and are echoing the raw post data.

You need to output your code using a proper Loop or explicitly process the shortcode.

echo do_shortcode($welcomePage -> post_content);
1
  • Did the trick, thanks! Good explanation as well :)
    – wickywills
    Commented Feb 11, 2014 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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