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.

Possible Duplicate:
Shortcode always displaying at the top of the page

I have my shortcode system set up fairly normally, like so:

//[carouselle]
function carouselle_func( $atts ){
 return "foo and bar";
}

add_shortcode( 'carouselle', 'carouselle_func' );

But when I do the same thing but with an echo statement instead of a return...

//[carouselle]
function carouselle_func( $atts ){
 echo "foo and bar";
}

add_shortcode( 'carouselle', 'carouselle_func' );

it displays "foo and bar" inside the post and also immediately the body tag.

the echo statement

I checked my header and I'm not calling the_content anywhere near there. So, is this because I'm doing an echo? I was hoping to eventualy do something like this:

//[carouselle]
function carouselle_func( $atts ){
 ?> 
 <h1>Some HTML Stuff</h1>
 <?php 
}

add_shortcode( 'carouselle', 'carouselle_func' ); 

But when this code executes, what happens is

Some HTML stuff

gets displayed near the top of my theme and also in the content of the post. Bizarre, or is this not even allowed?

share|improve this question

marked as duplicate by toscho Apr 19 '12 at 3:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Please use the search before you post. And never use just the DOM view to see what's going on, view the real source instead. –  toscho Apr 19 '12 at 3:15

1 Answer 1

Shortcode functions are supposed to "return" the value, not echo it: http://codex.wordpress.org/Shortcode_API

//[carouselle]
function carouselle_func( $atts ){
 return "<h1>Some HTML Stuff</h1>";
}
add_shortcode( 'carouselle', 'carouselle_func' ); 
share|improve this answer
    
Ok thanks! That makes sense –  21zna9 Apr 18 '12 at 21:06

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