• Resolved ace0930

    (@ace0930)


    I wanna create a form with a shortcode so I can use it wherever I need it. I use the below minimal example:

    
    add_shortcode( 'form', 'create_form' );
    function create_form () {
        require_once __DIR__ . '/form.php';
    }
    

    But when I add the shortcode on the post and try to save it, it shows an error: ‘Updating failed. The response is not a valid JSON response.’

    I have also noticed that WordPress specifies this: ‘shortcode should never produce an output of any kind’.

    If the shortcode is not meant for making such a task, how do I make a form and add it somewhere? Or the shortcode is okay but I misunderstood and doing something wrong?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    A shortcode RETURNS its result. It does not echo or write directly to the screen. So, you can certainly produce forms through a shortcode; many forms plugins do eactly that.

    So, the question is what’s in your “form.php” file?

    Also, doing forms securely is not an easy thing, so rather than coding your own forums from scratch, it might be better to use an existing forms plugin.

    Thread Starter ace0930

    (@ace0930)

    @sterndata There is an HTML form element only in form.php. Now I understand the concept, I need to return the HTML rather than directly require it. Many thanks.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The “lazy” way to do that is to start with an ob_start() and return (and remove) that buffer at the end. The only issue is that using the “ob*” functions may disrupt some caching methods.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add shortcode for form’ is closed to new replies.