Return vs Echo

I recently made a mistake that I wanted to share. It might save you a headache.

When making a short code make sure to return a value and not just echo it out.

The echo will display content, but it’s placement will be off, which is what gave me my headache.

Instead, put your echo’s in a string then return it.

//incorrect
function foobar_func( $atts ){
 echo "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
//correct
function foobar_func( $atts ){
 return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );