Shortcode adding plugin output before post, instead of inline

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:

Read More
(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.

Related posts

Leave a Reply

1 comment

  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;
    }