2

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.

2 Answers 2

1

You cannot (and shouldn't) call the php files in wp themes directly. One way to do this is to create landing.php and post-landing.php as wp_template files. Then create Pages using these templates from the Wp-admin. And then landing.php use the link of the pages created with that post-landing.php template as the action to the form.

Sign up to request clarification or add additional context in comments.

Comments

0

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):

add_action( 'wp_ajax_nopriv_<call_name>', '<function name>' );
add_action( 'wp_ajax_<call_name>', '<function name>' );

function <function_name>() {//here you do things with $_POST and return json}

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:

wp_localize_script( 'enqueued-js-name', 'varName', array(
    'ajaxUrl' => admin_url( 'admin-ajax.php' )
));

And then you can call it like this in your enqueued JS file:

jQuery.ajax({
    url: varName.ajaxUrl,
    type: 'post',
    data: currentDataToSubmit,
    success: function(response){
       //manage the response
    }
});

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!

2 Comments

I'm trying the easier way first, So i did <?php echo get_stylesheet_directory_uri() . '/templates/post-landing.php' ?> which gives correct url but still it says 404. Does it got to do with the permission ?
I did a check, I uncorrectly remembered i did it once, but all the steps were managed by the same page. Seems like Amit was totally right. I'll keep trying to understand what's wrong with that method, technically, but that could also be cause of wordpress' htaccess rules! For now, you have "my" way or Amit's one. I apologize!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.