Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
share|improve this question
    
Works for me. What is the point of add_action( 'init', 'register_shortcodes');? –  s_ha_dum Feb 11 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 Feb 11 at 15:04
    
@s_ha_dum - Just added my output code to my question if that helps? –  wickywills Feb 11 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 Feb 11 at 15:10

1 Answer 1

up vote 4 down vote accepted

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);
share|improve this answer
    
Did the trick, thanks! Good explanation as well :) –  wickywills Feb 11 at 15:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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