This question already has an answer here:

I develop my plugin with this code :

function my_function( $atts ) {
    some code
add_shortcode( 'myfunction_shortcode', 'my_function' );

I need to use my shortcode on my page or post, when I used it output code shown on top of my page.

marked as duplicate by Michael, Christine Cooper, Jacob Peattie, Dave Romsey, bosco Mar 1 '18 at 23:16

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.


This is most likely due to the way your shortcode is constructed. PHP scripts are run on the server before (as) the HTML is generated.

Try buffering your shortcode ouput like this:

function my_function( $atts ) {
    // do some stuff

    $code_output = ob_get_contents();

    return $code_output;

More info: https://secure.php.net/manual/en/book.outcontrol.php

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