recommended practice for form submission

I would use init hook in your plugin file to register custom function

add_action( 'init', 'custom_proccess_form' );

And then check form inputs in the custom function, you mentioned creating posts

function custom_proccess_form() {

    $wp_error = true;   // report errors or not
    $nonce = $_POST['_wpnonce'];

    if( isset($_POST['insert_post']) && wp_verify_nonce($nonce, 'my-nonce-name') ) {

        $post = array(
            'post_title' => $_POST['new_post_title'],
            'post_content' => $_POST['new_post_content'],
            );

        // form actions
        $post_id = wp_insert_post( $post, $wp_error );

        // now you can use $post_id within add_post_meta or update_post_meta

        // redirect at the end to some url
        wp_redirect('/?success=true');
    }
}

The form itself could look like this

<form method="POST">
    <?php wp_nonce_field('my-nonce-name'); ?>
    <input type="text" name="new_post_title">
    <textarea name="new_post_content"></textarea>
    <input type="hidden" name="insert_post" value="1">
    <button type="submit">Submit!</button>
</form>

Leave a Comment

tech