WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
short code output too early

How can I insert my plugin code via shortcode inside post body in exact position?

The problem is that wp engine inserts the plugin code BEFORE post body all the time. Can it be fixed?

For example: I have some [shortcode option="value"], that generates some text with image inside div container. When I put it inside the post AFTER the text, wp outputs this code:

<div> here is my plugin code </div> <p> here is the text of the post </p>

… and so it gets in the top of the post.

Where is my fault?

share|improve this question

marked as duplicate by toscho Dec 10 '12 at 10:13

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.

Are you using print or echo in your shortcode handler? Read this. – toscho Dec 10 '12 at 9:57
Yes, the problem was in echo's. Thank you for help. – drake2300 Dec 10 '12 at 10:12
up vote 1 down vote accepted

I have had this problem before: shortcodes shouldn't display any content (using print or echo), instead return the content to be outputted.

If it's too much trouble converting all of your output statements, or you need to use a function that will always display the output, you can use output buffering. A buffer will 'catch' any echo'd or print'd content and allow you to write it to a variable.

function my_awesome_shortcode( $atts, $content = null ) {
    ob_start(); // begin output buffering
    echo 'foo' . "bar\n";
    $greeting = 'Hello';
    printf( '%s, %s!', $greeting, 'World' );
    $output = ob_get_contents(); // end output buffering
    ob_end_clean(); // grab the buffer contents and empty the buffer
    return $output;

add_shortcode( 'awesome', 'my_awesome_shortcode' );

Learn more about Output Buffering Control and the different Output Control Functions that you can use.

share|improve this answer
Thanks a lot, return works exactly as I expected. – drake2300 Dec 10 '12 at 10:11

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