I have a couple of long forms which I want to output as shortcodes. Shortcode API indicates that

If the shortcode produces a lot of HTML then ob_start can be used to capture output and convert it to a string

This seems to be exactly my case. Also, I want to use WordPress functions inside my HTML, like this:

function my_shortcode_register_form( $atts, $content = null ) {

    <div id="reg_form">
        <h2><?php echo __('Login info', 'my-plugin-domain'); ?></h2>
        <!-- and so on -->

    <p><a href="<?php echo site_url( 'dashboard' ); ?>"><?php echo __('Go to Dashboard', 'my-plugin-domain'); ?></a>!</p>
    $output_string = ob_get_contents();

    return $output_string;

add_shortcode('register_form', array( $this, 'my_shortcode_register_form' ));

It works fine, but I'm interested in best-practices, potential bugs, as well as performance. Any downsides to this approach? Anything that could go awfully wrong?

I saw at least one question on here warning not only use output buffering unless absolutely necessary. Should I just bring these forms out into templates instead?

  • 1
    The question you found has the WordPress related part of this question. Otherwise, it seems like a Pure PHP question to me.
    – s_ha_dum
    Commented Mar 26, 2013 at 18:50
  • 2
    @s_ha_dum How about the question regarding using shortcode vs. custom template to avoid using output buffering altogether? Sounds pretty on-topic as far as FAQ is concerned. Commented Mar 26, 2013 at 19:10

1 Answer 1


Should I just bring these forms out into templates instead?

It depends on the form, and you haven't printed much detail about what your forms do. Shortcodes are nice but are not the right tool for every job.

I would:

  1. Prefer a dedicated template to a shortcode, especially for long forms. You start to get into having to handled $_POST or $_GET data in the shortcode or check for the presence of that data in your header, or other such, and that gets messy. Plus the shortcode is overhead that may be avoidable.
  2. Consider an exception to Rule #1 and make a shortcode, if the form has to be reused on multiple pages, or if such reuse is at least a plausible possibility. Honestly, if it fails the "reuse" test I don't see the point of the shortcode at all.
  3. Consider an exception to Rule #1 and #2 if you need editable content around the shortcode.
  4. If you are writing a plugin, a shortcode is one of your better choices. You could instruct people to create a page template and insert some code, or alter rewrite rules to make your own pseudo-page but the first is off-putting for most users and the second is complex. Even asking people to paste a shortcode into the post body can cause a panic, but at some point humanity has to learn to use computers.

I am sure there are other concerns but those are the ones that stand out to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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