Support » Fixing WordPress » Plugin called via shortcode appears at the wrong place on post

  • Resolved scuderias

    (@scuderias)


    Hello,

    I am developing a plugin for my blog and trying to set it up to be called by a shortcode. The plugin works, however when I call it via shortcode it appears at the top of the post, right below the title.

    This is my shortcode function:

    function my_func($atts) {
    extract(shortcode_atts(array(
    ‘foo’ => ‘no foo’,
    ‘bar’ => ‘default bar’,
    ), $atts));

    return;

    my_func_plugin();

    return “This line is below plugin”;
    }
    add_shortcode(‘myfunc’, ‘my_func’);

    So in the post it appears like this (I put the shortcode at the end of my post content):
    1. Post title
    2. my_func_plugin
    3. Post content
    4. The output of the return line in my_func (“This line is below plugin”)

    What am I missing for the plugin to appear after the post content?

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • After some research I discovered the solution, the output of the shortcode should be on the return line to appear at the correct place in the post.

    The problem now is that my plugin function “my_func_plugin()” echoes quite a few lines.

    The solution is to collect those lines in a variable and then use it on the return line.

    How can I do that? I am relatively new to PHP so I am having a hard time figuring it out. Something like this maybe?

    $output. = my_func_plugin();
    return $output;

    Any suggestions?

    Solution found! By capturing the output buffer… this is used in the shortcode handler function:

    ob_start();
    my_func_plugin();
    $output_string=ob_get_contents();;
    ob_end_clean();

    return $output_string;

    This way the plugin output appears at the correct position in the post.

    I hope this will be useful to someone else as well.

    Very useful. You may have saved me quite a bit of time. Thank you.

    Hello I had the same problem and your post gave me the idea to fix it. Instead of printing the return just return it!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Plugin called via shortcode appears at the wrong place on post’ is closed to new replies.