Question

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.

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top