How to call a plugin function from front end form

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.

Leave a Comment