1

I created a theme, in that theme, there is a custom form in the footer, The front end is good, But I don't know how to handle the back-end of the form submission in WordPress.

I mean I could create a handle_form.php in the simple website, Or handle the form on the same page, But what would be the best in WordPress, I can't change the layout of the form, So I think plugins won't help.

Should I handle the form in footer.php, Or any other better solution

Thanks

2

Although you can create your own code for handling form submissions, assuming your form is a contact-type form (not clear from your question), my solution would to be use a contact form plugin like Contact Form 7. It will create forms that can mail the data to you, and is simple to use. It does have 'hooks' that you can use if, for example, you wanted to store the form data in a database table. There are other questions here (with answers) on how to do that.

  • Thanks!, It works for me, I didn't know we can add classes to input elements in contact form7, I could format the front-end as I wanted. Thanks! – beginner Aug 15 '17 at 3:34
  • There's a lot you can do with Contact Form 7 - although the developer's info is sparse (like 'hook's and 'filters'). But being able to 'class' input elements helps make the form look better. I like using the developer's tools (Firefox/Firebug in my case) to preview quick CSS tweaks. (And if my answer is helpful, a 'checkmark' helps others solve similar problems.) – Rick Hellewell Aug 15 '17 at 5:57
1

You can create a REST route and process your form via that one. First step is to register a route. To do so, add this piece of code in your theme's functions.php file:

add_action( 'rest_api_init', function () {
    register_rest_route( 'beginner', '/submit_form/', array(
            'methods' => 'POST',
            'callback' => 'process_my_form'
    ) );
});

Now you change your form's action URL to this:

www.example.com/wp-json/beginner/submit_form

Make sure you set the method to POST too.

Now, you can create a callback function and process your form. Here is a sample:

function process_my_form( \WP_REST_Request $request ){
    // Now you have access to your form's values, for
    // example, you can use $username = $request['user'];
}

There you go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.