5

I currently have some custom post types "shops" (actually more like pages than posts) and a custom taxonomy "products" linked to those post types.

I'm attempting to create a form (via shortcode) that allows the user to select one (or potentially more in the future) products that when submitted, shows a page listing shops (title and other post meta) related to the chosen product(s).

I have the form, just not sure what the best way is to handle the submission and subsequent display page.

I noticed the codex page for add_rewrite_endpoint() states

This can be used for all sorts of things:

  • ajax handler
  • form submission handler
  • alternative notification handler

however there's no further information about how to achieve this.

Any pointers on handling form submissions would be great. Thanks.

Edit: Further on this, I want the form submission page to redirect to a nice URL representing the selected term. For example

  1. User selects product "books", hits submit
  2. Form submits to handling URL or some kind of hook catches the submission
  3. The browser is redirected to "shops/books"
  4. Something displays the "shop" pages tagged with "books" products

My question can be broken down to...

  1. What URL do I use in the form's action attribute? Is this even necessary?
  2. Where do I handle the form submission? Do I create a specific file or can I register some kind of action hook. Keep in mind that I'm wanting to send a Location header to redirect to another URL so this needs to happen prior to any output sent to the browser.
  3. How do I configure something to accept the "shops/%product%" request and show results accordingly

Sorry for the long-winded query but I'm struggling to get to grips with the Wordpress API and documentation.

1 Answer 1

5

This is actually really easy (at least #1 and #2 are):

  1. You can use any URL that loads a theme template file. For example, you could create a WordPress "Page" and in your Page Template you can use PHP's $_POST array to capture your <form> values.

  2. Unless you've got a good reason, you really don't need to do a redirect. You can, but I don't see that you need to and it just makes your task more complicated. If you really want to this answer will show you how.

  3. I'm confused by what you are trying to accomplish with your "shops/%product%" URL. At first blush your choice of "shops" for a custom post type and "products" for a taxonomy seems wrong to me (unless you are trying to represent many different shops; i.e. if your site is trying to be a marketplace for many different merchants. Is that the case? Even then, I still don't see why you'd use a taxonomy for your products.) So it seems to me you'd instead want to create a custom post type of "Product" (though I'm not sure about the taxonomy), and then you'll get URLs of the type products/%product% when you register you custom post type. Or maybe I misunderstand what you are trying to do for #3?

2
  • Thanks for the tips Mike. #2 - The listing pages will also need to be accessible via regular GET request so I'd like the form submission URLs to match. Also, I prefer the PRG pattern. #3 - Yes, there are multiple "shops". Also the "shops/" prefix is arbitrary. It could easily be "products" or "bananas" for all it matters ;-)
    – Phil
    Commented Nov 29, 2010 at 21:35
  • @Phil Brown - If you want accessibility via GET just used $_GET instead of $_POST. As for the PRG pattern (which I had to google) the link in #2 shows you what you need for that. As for arbitrary URL names I must admit that I frequently find myself confused by things that don't confuse other merely based on the names that are used. I think my brain is wired such that I need to understand how the use-case properly applies to be able to understand the use-case, and for that well-chosen names are crucial. FWIW. Commented Nov 29, 2010 at 21:48

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.