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' );

By Matt Christensen

I am a professional web / software developer located in Pontiac, IL. I have been developing web solutions and applications since 2003. I graduated from Bradley University (Peoria, IL) with a degree in Computer Science and Math. I currently work in Digital Marketing for a large Midwest company. However, I am available for projects. Please use the contact form if you have a project/site you need help with. Specialties: HTML, PHP, Linux, MySQL, CSS, WordPress, Joomla!, JavaScript, C#