Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

WP post form

By: a guest on Mar 29th, 2011  |  syntax: PHP  |  size: 5.55 KB  |  views: 296  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Template Name: Rate Wine Form
  4. */
  5. ?>
  6. <?php
  7. if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
  8.  
  9.     // Do some minor form validation to make sure there is content
  10.     if (isset ($_POST['title'])) {
  11.         $title =  $_POST['title'];
  12.     } else {
  13.         echo 'Please enter the wine name';
  14.     }
  15.     if (isset ($_POST['description'])) {
  16.         $description = $_POST['description'];
  17.     } else {
  18.         echo 'Please enter some notes';
  19.     }
  20.  
  21.     $tags = $_POST['post_tags'];
  22.     $winerating = $_POST['winerating'];
  23.  
  24.     // ADD THE FORM INPUT TO $new_post ARRAY
  25.     $new_post = array(
  26.     'post_title'    =>   $title,
  27.     'post_content'  =>   $description,
  28.     'post_category' =>   array($_POST['cat']),  // Usable for custom taxonomies too
  29.     'tags_input'    =>   array($tags),
  30.     'post_status'   =>   'publish',           // Choose: publish, preview, future, draft, etc.
  31.     'post_type' =>   'post',  //'post',page' or use a custom post type if you want to
  32.     'winerating'    =>   $winerating
  33.     );
  34.  
  35.     //SAVE THE POST
  36.     $pid = wp_insert_post($new_post);
  37.  
  38.              //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
  39.     wp_set_post_tags($pid, $_POST['post_tags']);
  40.  
  41.     //REDIRECT TO THE NEW POST ON SAVE
  42.     $link = get_permalink( $pid );
  43.     wp_redirect( $link );
  44.  
  45.     //ADD OUR CUSTOM FIELDS
  46.     add_post_meta($pid, 'rating', $winerating, true);  
  47.  
  48.     //INSERT OUR MEDIA ATTACHMENTS
  49.     if ($_FILES) {
  50.         foreach ($_FILES as $file => $array) {
  51.         $newupload = insert_attachment($file,$pid);
  52.         // $newupload returns the attachment id of the file that
  53.         // was just uploaded. Do whatever you want with that now.
  54.         }
  55.  
  56.     } // END THE IF STATEMENT FOR FILES
  57.  
  58. } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
  59.  
  60. //POST THE POST YO
  61. do_action('wp_insert_post', 'wp_insert_post');
  62.  
  63. ?>
  64.  
  65. <?php get_header(); ?>
  66.  
  67.         <div id="container">
  68.             <div id="content" role="main">
  69.  
  70. <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
  71.  
  72.                 <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  73.                     <?php if ( is_front_page() ) { ?>
  74.                         <h2 class="entry-title"><?php the_title(); ?></h2>
  75.                     <?php } else { ?>
  76.                         <h1 class="entry-title"><?php the_title(); ?></h1>
  77.                     <?php } ?>
  78.  
  79.                     <div class="form-content">
  80.                         <?php the_content(); ?>
  81.  
  82.         <!-- WINE RATING FORM -->
  83.  
  84.         <div class="wpcf7">
  85.         <form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
  86.             <!-- post name -->
  87.             <fieldset name="name">
  88.                 <label for="title">Wine Name:</label>
  89.                 <input type="text" id="title" value="" tabindex="5" name="title" />
  90.             </fieldset>
  91.  
  92.             <!-- post Category -->
  93.             <fieldset class="category">
  94.                 <label for="cat">Type:</label>
  95.                 <?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?>
  96.             </fieldset>
  97.  
  98.             <!-- post Content -->
  99.             <fieldset class="content">
  100.                 <label for="description">Description and Notes:</label>
  101.                 <textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>
  102.             </fieldset>
  103.  
  104.             <!-- wine Rating -->
  105.             <fieldset class="winerating">
  106.                 <label for="winerating">Your Rating</label>
  107.                 <input type="text" value="" id="winerating" tabindex="20" name="winerating" />
  108.             </fieldset>
  109.  
  110.             <!-- images -->
  111.             <fieldset class="images">
  112.                 <label for="bottle_front">Front of the Bottle</label>
  113.                 <input type="file" name="bottle_front" id="bottle_front" tabindex="25" />
  114.             </fieldset>
  115.  
  116.             <fieldset class="images">
  117.                 <label for="bottle_rear">Back of the Bottle</label>
  118.                 <input type="file" name="bottle_rear" id="bottle_rear" tabindex="30" />
  119.             </fieldset>
  120.  
  121.             <!-- post tags -->
  122.             <fieldset class="tags">
  123.                 <label for="post_tags">Additional Keywords (comma separated):</label>
  124.                 <input type="text" value="" tabindex="35" name="post_tags" id="post_tags" />
  125.             </fieldset>
  126.  
  127.             <fieldset class="submit">
  128.                 <input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" />
  129.             </fieldset>
  130.  
  131.             <input type="hidden" name="action" value="new_post" />
  132.             <?php wp_nonce_field( 'new-post' ); ?>
  133.         </form>
  134.         </div> <!-- END WPCF7 -->
  135.  
  136.         <!-- END OF FORM -->
  137.                         <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
  138.                         <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
  139.                     </div><!-- .entry-content -->
  140.                 </div><!-- #post-## -->
  141.  
  142.                 <?php comments_template( '', true ); ?>
  143.  
  144. <?php endwhile; // end of the loop. ?>
  145.  
  146.             </div><!-- #content -->
  147.        </div><!-- #container -->
  148.  
  149. <?php get_sidebar(); ?>
  150. <?php get_footer(); ?>