We have had a variety of posts about posting from the front-end. I had gotten us started with a series of posts. After getting a solid foundation in place, I started back to school and life got crazy. I had to switch to Gravity Forms for my own needs, I needed some pretty advanced forms, and just didn’t have the time to keep maintaining the code I had begun. At that time, I asked my readers to please continue developing this code and hit me up with any new features. That started a while back with our edit and delete code from fitoussi. Next up we have some code from Lisa of WP Axis who sent in a solution to get our validation working properly.
Here is the code she sent in, which she has developed and running on her site. I’ve tested out her form, and everything works great for me! I won’t describe too much in detail here what is going on, she has the section appropriately commented. You can take a look here and compare it to the old version of our form, the code is pretty simple and it just plain works!
<?php
/*
Template Name: Rate Wine Form
*/
?>
<?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['submit'])) {
$error = "";
if (!empty($_POST['title'])) {
$title = $_POST['title'];
} else {
$error .= "Please add a title<br />";
}
if (!empty($_POST['description'])) {
$description = $_POST['description'];
} else {
$error .= "Please add a description<br />";
}
if (!empty($_POST['post_tags'])) {
$post_tags = $_POST['post_tags'];
} else {
$error .= "Please add some keywords<br />";
}
if (!empty($_POST['winerating'])) {
$post_tags = $_POST['winerating'];
} else {
$error .= "Please add some keywords<br />";
}
// IMAGE VALIDATION - CHECK IF THERE IS AN IMAGE AND THAT ITS THE RIGHT FILE TYPE AND RIGHT SIZE
if ($_FILES) {
foreach ($_FILES as $file => $array) {
//Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)
if(isset($_FILES[$file]) && ($_FILES[$file]['size'] > 0)) {
$tmpName = $_FILES[$file]['tmp_name'];
list($width, $height, $type, $attr) = getimagesize($tmpName);
if($width!=630 || $height!=580)
{
$error .= "Image is to small<br />";
unlink($_FILES[$file]['tmp_name']);
}
// Get the type of the uploaded file. This is returned as "type/extension"
$arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
$uploaded_file_type = $arr_file_type['type'];
// Set an array containing a list of acceptable formats
$allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
// If the uploaded file is the right format
if(in_array($uploaded_file_type, $allowed_file_types)) {
} else { // wrong file type
$error .= "Please upload a JPG, GIF, or PNG file<br />";
}
} else {
$error .= "Please add an image<br />";
}
} // end for each
} // end if
$tags = $_POST['post_tags'];
$winerating = $_POST['winerating'];
// ADD THE FORM INPUT TO $new_post ARRAY
if (empty($error)) {
$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 SAVING POST
} // END VALIDATION
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');
?>
<?php get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } else { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>
<div class="form-content">
<?php
if (!empty($error)) {
echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
} elseif (!empty($success)) {
echo '<p class="success">' . $success . '</p>';
}
?>
<?php the_content(); ?>
<!-- WINE RATING FORM -->
<div class="wpcf7">
<form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
<!-- post name -->
<fieldset name="name">
<label for="title">Wine Name:</label>
<input type="text" id="title" value="" tabindex="5" name="title" />
</fieldset>
<!-- post Category -->
<fieldset class="category">
<label for="cat">Type:</label>
<?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?>
</fieldset>
<!-- post Content -->
<fieldset class="content">
<label for="description">Description and Notes:</label>
<textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>
</fieldset>
<!-- wine Rating -->
<fieldset class="winerating">
<label for="winerating">Your Rating</label>
<input type="text" value="" id="winerating" tabindex="20" name="winerating" />
</fieldset>
<!-- images -->
<fieldset class="images">
<label for="bottle_front">Front of the Bottle</label>
<input type="file" name="bottle_front" id="bottle_front" tabindex="25" />
</fieldset>
<fieldset class="images">
<label for="bottle_rear">Back of the Bottle</label>
<input type="file" name="bottle_rear" id="bottle_rear" tabindex="30" />
</fieldset>
<!-- post tags -->
<fieldset class="tags">
<label for="post_tags">Additional Keywords (comma separated):</label>
<input type="text" value="" tabindex="35" name="post_tags" id="post_tags" />
</fieldset>
<fieldset class="submit">
<input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" />
</fieldset>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div> <!-- END WPCF7 -->
<!-- END OF FORM -->
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Hopefully this gets you one step closer to submittal form perfection! Many thanks to Lisa for submitting this! Why not check out her site at WP Axis, it’s in the process of becoming a one stop WP resource for anything you could need! And for anyone else who has done anything amazing with fron-end posting, let’s see what you have.

Thanks a lot ! Perfect timing for my new website !
Please do an update in your older related (and obsolete) posts to add a link to this post ! It would have been helpful for me searching an up to date front-end form script/plugin…
A french guy
Done, thanks for the suggestion! The original post now links to this post, and the editing and deleting from the fron end post.
Hey Voodoo
Thanks for putting this up… I hope it helps out your readers. Your series on posting from the front end was the best I found anywhere after very relentless searching so once again thanks so much for all your help.
Take care
Lisa
And thank YOU for submitting this and helping out others searching for ways to pull this off! My series was the result of a LOT of wasted time at the Google machine. I am certain I read every available tutorial, or even tiniest relevant snippet of code to put everything together. Then, a lot of trial and error. It works really well, and I hope more people will work through this and offer further improvements.
Pingback: Bringing a WordPress Directory to Life - It Takes a Village | WP Axis
Hi! Cool tutorial thank you!
Just one question if i want to add some allowed formats (for example flv, avi etc.) how could i do that?
genio!! espero aprender mucho de ti… un abrazo
How i can add the imagen to the post? Because the image uploaded is not added to the wysiwyg editor of wordpress.
And thanks for this useful post!
I keep getting “Cannot modify header information – headers already sent by” etc. because if the user does not enter information in the form field boxes it echos the error message, which i guess it is not supposed to do before the header is sent via the redirect. Any ideas on how to stop this from happening?