Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Shortcode always displaying at the top of the page

I have a quick and dirty shortcode which outputs a form in my wordpress pages. However, it adds the shortcode output at the beginning of my page, instead of inline where I want it.

For example:

(WordPress post:)
This is some text!
[shortcode_here]
This is more text!

(output)
The end result does this:
Shortcode output is showing up here!
This is some text!
This is more text!

The shortcode function is simply pulling some info. from a database and echoing the formatted results, a la:

   function shortcode_output() {
      //get stuff from database
      //format stuff
      echo <<<FORM
      <form method="POST" action="blah.php">
      <!--more html-->
      </form>
   FORM;
   }

I tried returning the content instead of echoing, but that simply shows up as a 0.

share|improve this question

marked as duplicate by toscho Jun 1 '12 at 20:54

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.

1 Answer 1

You have to return html instead of echoing it. Buffer output withing your function and return it:

function shortcode_output() {
    ob_start();
    //get stuff from database
    //format stuff
    echo <<<FORM
    <form method="POST" action="blah.php">
    <!--more html-->
    </form>
FORM;

    $html = ob_get_contents();
    ob_end_clean();

    return $html;
}
share|improve this answer
    
Thanks! I did not know about the ob functions. That's perfect. –  Tom Thorogood Jun 1 '12 at 19:11
    
@TomThorogood you are welcome –  Eugene Manuilov Jun 1 '12 at 19:17
    
Avoid output buffering. Just don’t use echo. –  toscho Jun 1 '12 at 20:57

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