I want to create a form on a page but how do I handle the form submission.

Which hook should i implement to handle my submission? Probably dirtiest would be to do it in the theme itself but I don't want it that way... probably not the right way to do it

  • Are you just doing a contact form submission? What is the forms purpose? You handle forms in WordPress just like you would any other normal form - $_GET and $_POST. – Howdy_McGee Apr 17 '14 at 20:11
  • @bigant841 Are you talking about a custom form or are would you like to use a plugin form? – Howdy_McGee Apr 17 '14 at 20:12
  • it's a form that will create posts, comments with my plugin. – yeahman Apr 17 '14 at 20:27

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

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>
  • ok looks neat. Is there functions for flash messages? (session message that are displayed and auto erased on redirection) – yeahman Apr 18 '14 at 16:13
  • what is $wp_error used for? – yeahman Apr 18 '14 at 17:32
  • optional parameter for WP error, good for testing, see codex.wordpress.org/Function_Reference/wp_insert_post – rozklad Apr 19 '14 at 8:50
  • 1
    i dont use bbpress, but maybe you could lower the priority of the action by specifying something like add_action( 'init', 'custom_proccess_form', 100 ); – rozklad Apr 19 '14 at 9:38
  • 1
    i ended up using the hook wp_load and it works fine – yeahman Apr 19 '14 at 10:41

there is a plugin created to get through out your problem submitting the form. a plugin were over 8 out of 10 wordpress sites have it for free use contact-form-7 plugin by wordpress. these would make things easy

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.