Frontend Post Form Validation

OK, there have been several posts on here about frontend posting forms. I’ve read them all, which got me to where I am. Now every form I come across has some form of validation which seems to check for a value for the fields you want to make required. I’ve tried them, and I can’t get anything to work. I just can’t make any fields required. Everything else about the form is perfect. But I would love to enforce my required fields (bonus for appropriate error messages).

I’ve tried looking around on the google, I don’t really think JS is what I want to use for this. And any other php validation I can’t seem to make work either. I’m sure I’m incorporating them wrong somehow.

Read More

Here’s what I have: http://pastebin.com/rw4c6jZQ (full template lines 9-19 are supposed to be validation)

MY processing script at the to of the template:

    <?php 
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") { 

    // Do some minor form validation to make sure there is content 
    if (isset ($_POST['title'])) { 
        $title =  $_POST['title']; 
    } else { 
        echo 'Please enter the wine name'; 
    } 
    if (isset ($_POST['description'])) { 
        $description = $_POST['description']; 
    } else { 
        echo 'Please enter some notes'; 
    } 

    $tags = $_POST['post_tags']; 
    $winerating = $_POST['winerating']; 

    // ADD THE FORM INPUT TO $new_post ARRAY 
    $new_post = array( 
    'post_title'    =>   $title, 
    'post_content'  =>   $description, 
    'post_category' =>   array($_POST['cat']),  // Usable for custom taxonomies too 
    'tags_input'    =>   array($tags), 
    'post_status'   =>   'publish',           // Choose: publish, preview, future, draft, etc. 
    'post_type' =>   'post',  //'post',page' or use a custom post type if you want to 
    'winerating'    =>   $winerating
    ); 

    //SAVE THE POST 
    $pid = wp_insert_post($new_post); 

             //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL 
    wp_set_post_tags($pid, $_POST['post_tags']); 

    //REDIRECT TO THE NEW POST ON SAVE 
    $link = get_permalink( $pid ); 
    wp_redirect( $link ); 

    //ADD OUR CUSTOM FIELDS 
    add_post_meta($pid, 'rating', $winerating, true);  

    //INSERT OUR MEDIA ATTACHMENTS 
    if ($_FILES) { 
        foreach ($_FILES as $file => $array) { 
        $newupload = insert_attachment($file,$pid); 
        // $newupload returns the attachment id of the file that 
        // was just uploaded. Do whatever you want with that now. 
        } 

    } // END THE IF STATEMENT FOR FILES 

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM 

//POST THE POST YO 
do_action('wp_insert_post', 'wp_insert_post'); 

The form and everything else can be seen in the pastebin to see it all in context. Thanks much!

Related posts

Leave a Reply

2 comments

  1. The best way to do form validation is with a combination of JavaScript and PHP.

    The reason why you want to perform JavaScript validation is to give the users a chance to correct their errors without submitting the form. Plus, it will stop any casual mistakes. You will also want to ensure validation on the back-end in case JavaScript is turned off.

    Either way, sanitizing your data should be part of this process. Use any of these WordPress functions to help you there: Data Validation.

    For JavaScript validation, I recommend the jQuery Validation plugin. It’s super easy to implement and customize.

    As for your PHP validation, you can try adding this to check for empty values: $_POST['title'] != ''