So my app theme structure is
-wp-content
-themes
-cool
-templates
-landing.php
- post-landing.php
I have a form in landing.php and I want to handle POST request in post-landing.php for which I have,
form action="wp-content/themes/cool/templates/post-landing.php"
But when I make a post request it says 404.
You cannot (and shouldn't) call the php files in wp themes directly. One way to do this is to create
landing.php
andpost-landing.php
as wp_template files. Then createPages
using these templates from the Wp-admin. And thenlanding.php
use the link of the pages created with thatpost-landing.php
template as the action to the form.Here's another way: in case you want to manage that asynchronously, you can create an AJAX call like this(I usually place it in my
<theme>/inc
folder, so it's automatically loaded):Localize it in your
functions.php
file in order to have your ajaxUrl for the request always available in a variable and only for your js file:And then you can call it like this in your enqueued JS file:
And that's it! hope it was useful!
If there's something wrong with the first part, let me know, so I can learn too!