0

So everything is working fine except for the validation. The error messages don't display when the form is submitted empty and you can still submit the form when empty. I must be missing something really obvious. I've already spent a few hours scouring the web and have tried several fixes myself to no avail. Any help would be really appreciated.

<?php add_shortcode('testimonial-form', 'testimonial_form_sc') ?>

<?php function testimonial_form_sc(){ ?>

<div class="row">
<h3>Send Us Your Testimonial</h3>
<form id="add_testimonial" name="add_testimonial" method="post" action="" enctype="multipart/form-data">

<!-- post name -->

    <label for="username" style="display:block">Name:</label>
    <input type="text" value="" name="username" style="display-block" />

<!-- post content -->
    <label for="description" style="display:block">Testimonial:</label>
    <textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>

    <input type="file" name="thumbnail" id="thumbnail">

    <input class="small-button" type="submit" value="Send" id="submit" name="submit" style="display:block;"/>

<input type="hidden" name="action" value="add_testimonial" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>

</div> 

<?php

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&     $_POST['action'] == "add_testimonial") {

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

// add the form input to $new_testimonial array
$new_testimonial = array(
'post_title'    =>   $title,
'post_content'  =>   $description,
'post_status'   =>   'publish',           // Switch this to draft if you want to moderate posts
'post_type' =>   'testimonial'  //post type to insert the post into
);

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

// Upload image for post thumbnail

 if (!function_exists('wp_generate_attachment_metadata')){
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file]['error'];
                }
                $attach_id = media_handle_upload( $file, $pid );
            }   
        }
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($pid,'_thumbnail_id',$attach_id);
        }

include 'validate-image-size.php';

//Redirect to the new post on save
$link = get_permalink( $pid );
wp_redirect( $link );

}} // End of the if statement that started the form and the end of the shortcode     [testimonial-form]

//post the post
do_action('wp_insert_post', 'wp_insert_post');`
1

Your validation is not working because the variables with the empty array return true with isset;

Go to http://writecodeonline.com/php/ and try this code, and you'll understand:

$test = "";
if ( isset($test) ){
    echo "true";
}

The right function for it, instead of isset is empty. Try it out:

$test = "";
if ( empty($test) ){
    echo "empty";
}

Further, if you're doing lots of modelling and validations in wordpress, I suggest using the wonderfull PHPActiveRecord. I learned how to use it here, and I'm in love with it. :)

  • That makes sense now! Thanks for your help. I looked at PHPActiveRecord and it looks great. I'll look into a little further today. Thanks again! – user57391 Aug 10 '14 at 18:18
  • Of course, you can use jQuery validations, if you need something quicker, but, since I came from Ruby On Rails, I really enjoyed working with this modeling architecture. I've put this on wordpress and it's helping a lot! – Mauricio Moraes Aug 11 '14 at 0:29

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.