I have a page template having a form which further have 3 input fields and and a submit button. I have a written a function in a plugin which inserts the retrieved values in database. I am trying to figure out what to write in action attribute of the form, so that when user clicks the submit button, the function gets executed?

2 Answers
2

Output The Form

Can be done via Shortcode API.

What Should Be Set on Form Action

Leave it blank <form action="">. Make sure you avoid using reserved name as form field name like name or p. Otherwise your form submission will hit 404.

How Can We Validate or Process The Form?

Hook into template_redirect.

add_action( 'template_redirect', 'wpse149613_form_process' );

function wpse149613_form_process(){
    if(!isset($_POST['submit'])) // assuming you're using POST and submit button name is 'submit'
        return;

    // Validate the form, verify nonce

    // process form

}

Hope it makes sense. This is how I usually handle form submission.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *