Advertisement
Guest User

WordPress - Post from Frontend

a guest
Jul 29th, 2014
1,879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.66 KB | None | 0 0
  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['submit'])) {
  11.             $error = "";
  12.  
  13.         if (!empty($_POST['title'])) {
  14.             $title = $_POST['title'];
  15.      } else {
  16.         $error .= "Please add a title<br />";
  17.     }
  18.  
  19.         if (!empty($_POST['description'])) {
  20.             $description = $_POST['description'];
  21.      } else {
  22.         $error .= "Please add a description<br />";
  23.     }
  24.  
  25.         if (!empty($_POST['post_tags'])) {
  26.             $post_tags = $_POST['post_tags'];
  27.      } else {
  28.         $error .= "Please add some keywords<br />";
  29.     }
  30.  
  31.         if (!empty($_POST['winerating'])) {
  32.             $post_tags = $_POST['winerating'];
  33.      } else {
  34.         $error .= "Please add some keywords<br />";
  35.     }
  36.         // IMAGE VALIDATION - CHECK IF THERE IS AN IMAGE AND THAT ITS THE RIGHT FILE TYPE AND RIGHT SIZE
  37.         if ($_FILES) {
  38.             foreach ($_FILES as $file => $array) {
  39.                 //Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)
  40.  
  41.                 if(isset($_FILES[$file]) && ($_FILES[$file]['size'] > 0)) {
  42.  
  43.                     $tmpName = $_FILES[$file]['tmp_name'];
  44.                     list($width, $height, $type, $attr) = getimagesize($tmpName);
  45.  
  46.                 if($width!=630 || $height!=580)
  47.                 {
  48.                     $error .= "Image is to small<br />";
  49.                     unlink($_FILES[$file]['tmp_name']);
  50.                 }
  51.  
  52.                 // Get the type of the uploaded file. This is returned as "type/extension"
  53.                 $arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
  54.                 $uploaded_file_type = $arr_file_type['type'];
  55.  
  56.                  // Set an array containing a list of acceptable formats
  57.                 $allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
  58.  
  59.                  // If the uploaded file is the right format
  60.                 if(in_array($uploaded_file_type, $allowed_file_types)) {
  61.  
  62.                 } else { // wrong file type
  63.                 $error .= "Please upload a JPG, GIF, or PNG file<br />";
  64.                      }
  65.  
  66.                 } else {
  67.                 $error .= "Please add an image<br />";
  68.                 }
  69.             } // end for each
  70.         } // end if
  71.  
  72.         $tags = $_POST['post_tags'];
  73.         $winerating = $_POST['winerating'];
  74.  
  75.         // ADD THE FORM INPUT TO $new_post ARRAY
  76.         if (empty($error)) {
  77.             $new_post = array(
  78.             'post_title'    =>  $title,
  79.             'post_content'  =>  $description,
  80.             'post_category' =>  array($_POST['cat']),  // Usable for custom taxonomies too
  81.             'tags_input'    =>  array($tags),
  82.             'post_status'   =>  'publish',           // Choose: publish, preview, future, draft, etc.
  83.             'post_type' =>  'post',  //'post',page' or use a custom post type if you want to
  84.             'winerating'    =>  $winerating
  85.         );
  86.  
  87.         //SAVE THE POST
  88.         $pid = wp_insert_post($new_post);
  89.  
  90.         //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
  91.         wp_set_post_tags($pid, $_POST['post_tags']);
  92.  
  93.         //REDIRECT TO THE NEW POST ON SAVE
  94.         $link = get_permalink( $pid );
  95.         wp_redirect( $link );
  96.  
  97.         //ADD OUR CUSTOM FIELDS
  98.         add_post_meta($pid, 'rating', $winerating, true);
  99.  
  100.             //INSERT OUR MEDIA ATTACHMENTS
  101.             if ($_FILES) {
  102.                 foreach ($_FILES as $file => $array) {
  103.                 $newupload = insert_attachment($file,$pid);
  104.                 // $newupload returns the attachment id of the file that
  105.                     // was just uploaded. Do whatever you want with that now.
  106.                 }
  107.  
  108.             } // END THE IF STATEMENT FOR FILES
  109.         } // END SAVING POST
  110.     } // END VALIDATION
  111. } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
  112.  
  113. //POST THE POST YO
  114. do_action('wp_insert_post', 'wp_insert_post');
  115.  
  116. ?>
  117.  
  118. <?php get_header(); ?>
  119.  
  120.         <div id="container">
  121.             <div id="content" role="main">
  122.  
  123. <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
  124.  
  125.                 <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  126.                     <?php if ( is_front_page() ) { ?>
  127.                         <h2 class="entry-title"><?php the_title(); ?></h2>
  128.                     <?php } else { ?>
  129.                         <h1 class="entry-title"><?php the_title(); ?></h1>
  130.                     <?php } ?>
  131.  
  132.                     <div class="form-content">
  133.                      <?php
  134.                         if (!empty($error)) {
  135.                             echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
  136.                         } elseif (!empty($success)) {
  137.                             echo '<p class="success">' . $success . '</p>';
  138.                         }
  139.                     ?>
  140.                         <?php the_content(); ?>
  141.  
  142.         <!-- WINE RATING FORM -->
  143.  
  144.         <div class="wpcf7">
  145.         <form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
  146.             <!-- post name -->
  147.             <fieldset name="name">
  148.                 <label for="title">Wine Name:</label>
  149.                 <input type="text" id="title" value="" tabindex="5" name="title" />
  150.             </fieldset>
  151.  
  152.             <!-- post Category -->
  153.             <fieldset class="category">
  154.                 <label for="cat">Type:</label>
  155.                 <?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?>
  156.             </fieldset>
  157.  
  158.             <!-- post Content -->
  159.             <fieldset class="content">
  160.                 <label for="description">Description and Notes:</label>
  161.                 <textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>
  162.             </fieldset>
  163.  
  164.             <!-- wine Rating -->
  165.             <fieldset class="winerating">
  166.                 <label for="winerating">Your Rating</label>
  167.                 <input type="text" value="" id="winerating" tabindex="20" name="winerating" />
  168.             </fieldset>
  169.  
  170.             <!-- images -->
  171.             <fieldset class="images">
  172.                 <label for="bottle_front">Front of the Bottle</label>
  173.                 <input type="file" name="bottle_front" id="bottle_front" tabindex="25" />
  174.             </fieldset>
  175.  
  176.             <fieldset class="images">
  177.                 <label for="bottle_rear">Back of the Bottle</label>
  178.                 <input type="file" name="bottle_rear" id="bottle_rear" tabindex="30" />
  179.             </fieldset>
  180.  
  181.             <!-- post tags -->
  182.             <fieldset class="tags">
  183.                 <label for="post_tags">Additional Keywords (comma separated):</label>
  184.                 <input type="text" value="" tabindex="35" name="post_tags" id="post_tags" />
  185.             </fieldset>
  186.  
  187.             <fieldset class="submit">
  188.                 <input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" />
  189.             </fieldset>
  190.  
  191.             <input type="hidden" name="action" value="new_post" />
  192.             <?php wp_nonce_field( 'new-post' ); ?>
  193.         </form>
  194.         </div> <!-- END WPCF7 -->
  195.  
  196.         <!-- END OF FORM -->
  197.                         <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
  198.                         <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
  199.                     </div><!-- .entry-content -->
  200.                 </div><!-- #post-## -->
  201.  
  202.                 <?php comments_template( '', true ); ?>
  203.  
  204. <?php endwhile; // end of the loop. ?>
  205.  
  206.             </div><!-- #content -->
  207.         </div><!-- #container -->
  208.  
  209. <?php get_sidebar(); ?>
  210. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement