shortcodes displaying outside of content area with echo statement, not what I wanted

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…

Read More
//[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?

Related posts

Leave a Reply

1 comment