[Solved] WordPress Shortcode Always Appear on the Top of Content

WordPress shortcode is a feature that makes it easy for developers themes and plugins. With shortcode, we can insert form, element, script, or whatever it is in wordpress editor. It would also allow a user to manage his website.

But there are little problems that have a major impact, namely the content of shortcode always appear at the top of the content. Whereever you put the shortcode, still, the contents of the shortcode will always appear on the top.

<?php
 function my_shortcode(){
 echo 'Hello world';
}

add_shortcode( 'mycode', 'my_shortcode' );
?>

Then, you put the wherever [mycode] in the content,  Hello World will appear in  the top of content. What happened?

Apparently, shortcode should not use echo, because echo prints on the screen.

Shall in your code like this

<?php
 function my_shortcode(){
 return 'Hello world';
}

add_shortcode( 'mycode', 'my_shortcode' );
?>

The above code is the correct code. I use wordpress 4.0 and above and successfully. If not successful, maybe you use wordpress versions differ with you.

I hope this short article will help you. Dont forget to comment below and share this small code to your social network. Thanks you very much

Leave a Reply

Your email address will not be published. Required fields are marked *