Support » Developing with WordPress » Using wp_redirect in shortcode based contact form

  • Resolved Guido

    (@guido07111975)


    Hi there,

    I have a shortcode which containes a contact form.

    I would like to add the wp_redirect() (or header:location) directly after successful form validation. But I run into the “headers already sent” warning. I know why, and the only way I can avoid this is to handle my form much earlier.

    So I found this thread.

    But I don’t know how to integrate this into my shortcode. Should this be inside my shortcode, or the other way around? Or something else? Or should I avoid this approach? Who can help me with this?

    Guido

Viewing 5 replies - 1 through 5 (of 5 total)
  • As you cannot do an action after the return statement inside your shortcode function, my idea would be to solve it via the action attribute in the form tag.

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#action

    Guido

    (@guido07111975)

    I don’t understand how it’s possible to handle a form submission before the form is rendered on a page. Because as far as I understand this actually happens, when using the solution in that thread. But I might be mistaken.

    Guido

    That solution is exactly what you need.
    The wp_loaded action is much earlier in the sequence of actions than where the form is actually rendered. It is standard procedure to check for your form submission very early, and act on it, and then later the form is rendered (or not if you redirected to another page).
    See https://codex.wordpress.org/Plugin_API/Action_Reference for a list in sequence of the actions you can hook to.

    Moderator bcworkz

    (@bcworkz)

    Hiya Guido,

    It sounds to me like you are trying to accomplish it all within your shortcode handler. Because of where/when this code executes, it’s simply not possible to PHP redirect or send headers of any sort from a shortcode handler. It’s much too late. As has been indicated by the above learned replies, the form handling and conditional redirect needs to be handled earlier and separately from the shortcode.

    Another alternative would be to use a JavaScript redirect (location.href property), which can occur anytime. As we recently discussed, JS can be altered or stopped by the user, so is insecure and possibly non-functional for some. For non-sensitive purposes, handling the form client side and sending whatever data is required server side via Ajax before redirecting is a viable alternative. This is essentially how CF7 redirects are handled FWIW.

    Guido

    (@guido07111975)

    Hi,

    Thank you for your replies! Much clearer now.

    If I understand correctly I should add an action in my form and do whole form handling in a separate function (or file), outside my shortcode.

    I’m gonna try this!

    @bcworkz: I want to avoid using js in my form… so that’s why I prefer using the wp_redirect function. Want to reload (reset) the contact page after successful validation, to avoid double form submission when user does a page refresh.

    Guido

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using wp_redirect in shortcode based contact form’ is closed to new replies.