Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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!

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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'] != ''

share|improve this answer
    
I'll have to do some googling looks like. I'm really trying to avoid any plugins. I mean, if I use a plugin for the validation, I might as well have used one for the whole form... –  Rev. Voodoo Mar 31 '11 at 11:34
    
Also, I've used that php check, I just must be using it wrong in my code, as it's not doing anything.... –  Rev. Voodoo Apr 4 '11 at 11:13
    
Why avoid a tool that does exactly what you need it to do with minimal effort? A jQuery plugin isn't the same thing as a WordPress plugin, so there's not much overhead. –  Matthew Muro Apr 4 '11 at 14:08
    
I'll work with it and see what I get, thanks for the input.... –  Rev. Voodoo Apr 4 '11 at 16:04

i think, the right way via php is the esc_attr* functions of WordPress and all function in this file: wp-includes/formatting.php

Also use the possibilities from PHP: filter_var

share|improve this answer
    
I'll look that all over, to be honest right now, I have no idea how to use any of that. But I'll see what the codex and google can turn up..... But I'm always open to suggestion if anyone has any? Thanks for the leads! –  Rev. Voodoo Mar 30 '11 at 11:01
    
A lot of that looks like its for sanitizing the input? I'm not really after that at this point. I'm just currently looking for a way to check if required fields have been filled out. If they have, submit the form. If not, display error messages. The filter_var looks promising, but I can't seem to figure it out for my needs. No matter what I do, the form displays with the error message right away, and I can't figure out how to use it to stop the form from posting if required fields are empty. –  Rev. Voodoo Mar 30 '11 at 11:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.