What is the best way to process a form in WP using correct design patterns? I am finding a lot of outdated and inconsistent information online and the way I am doing it now is not sustainable.

At the moment, when I am doing some heavy PHP work (ie.. processing large forms) I tend to create a page in WordPress, flesh out the HTML as much as I can and then create a custom template.. within the template I then add the form processing PHP stuff... this works well but duplicates a lot of template code and if it's a large site I end up with lots of custom templates.

Other solutions i've seen online:

  • create a function and then add it to the init filter, add a hidden form field in the html so that the form is specifically targeted.. this looks like it will try to process this request on each page load, so doesn't look like viable solution.

  • Create a plugin - as above, I don't really want to create a full new plugin just to process a form.

  • Create a separate PHP file called process and just stuck the PHP stuff in here, this seems like it removes itself from WP all together, i've actually seen some examples have this completely independent as it even has the mysql connect stuff in there.

[edit] Here is my form submission function: http://pastebin.com/VZCw3Br3 It's a bit of a mess so I have commented it as much as possible.

1 Answer 1


"create a plugin" isn't really a separate option, you'd still need to intercept requests somehow. I've used the init method, and it's a viable option. Checking isset on a POST var before kicking off your processing logic is extremely fast, I doubt you could even reliably measure the impact in the sea of other things going on in the load process. The other option is admin-post.php and the admin_post_(action) hook. That method would require redirection after processing.

  • I decided to throw this into a plugin but still struggling to submit the form. It just says Page cannot be displayed.. What should be in the 'action' property of the form?
    – Goodbytes
    Commented Jun 25, 2015 at 9:37
  • Here is my form submission function: pastebin.com/VZCw3Br3 It's a bit of a mess so I have commented it as much as possible.
    – Goodbytes
    Commented Jun 25, 2015 at 10:01
  • not sure exactly what the issue is, but I see a couple of potential issues. you can't use post vars that are WordPress query vars, for example name. Prefix everything with a unique string to guarantee uniqueness. you also can't generate any output on the init action, you will step on any subsequent code that sets headers.
    – Milo
    Commented Jun 25, 2015 at 18:22
  • Thanks, I will update code accordingly.. what is the correct action to hook the form submission onto? at the moment when I submit the form it gives me a 404 page cannot be displayed error.
    – Goodbytes
    Commented Jun 26, 2015 at 7:18
  • 1
    If you're using name as a POST var, WordPress is trying to query for a post with a slug that matches that value. That's why you can't use any reserved terms as form element names, WordPress picks those up and it changes the main query.
    – Milo
    Commented Jun 26, 2015 at 8:26

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.