Advertisement
Guest User

Insert post WordPress frontend v2

a guest
Mar 7th, 2011
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Template Name: Post Insert
  4.  *
  5.  * A custom page template that allows users to submit a post from the front end.
  6.  * Based on the Twenty Ten page template.
  7.  * The "Template Name:" bit above allows this to be selectable
  8.  * from a dropdown menu on the edit page screen.
  9.  *
  10.  * @author iCosmin (http://icosmin.com)
  11.  * @package WordPress
  12.  * @subpackage Twenty_Ten
  13.  * @since Twenty Ten 1.0
  14.  */
  15.  
  16. get_header(); ?>
  17.  
  18.         <div id="container" class="one-column">
  19.             <div id="content" role="main">
  20.          
  21.             <?php
  22.                                      
  23.                 global $wpdb;
  24.                 global $post;
  25.                 $title = $_POST['title']; // get the inputted title
  26.                 $content = $_POST['content']; // get the inputted content
  27.                 $categorie = $_POST['cat'];  // get the category selected by user
  28.                 $zombies = $_POST['zombies'];
  29.                 # run a query to check for a post containing the data that our user is about to submit
  30.                 # store results in $verifica
  31.                 $sql = "
  32.                 SELECT wposts.*
  33.                 FROM $wpdb->posts wposts
  34.                     LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
  35.                     LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
  36.                     LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
  37.                 WHERE wposts.post_status = 'publish'
  38.                     AND wposts.post_title = '$title'
  39.                     AND wposts.post_content = '$content'
  40.                     AND wpostmeta.meta_key = 'Zombies'
  41.                     AND wpostmeta.meta_value = '$zombies'
  42.                     AND $wpdb->term_taxonomy.taxonomy = 'category'
  43.                     AND $wpdb->term_taxonomy.term_id IN($categorie)";
  44.                 $verifica = $wpdb->get_results($sql);
  45.                
  46.                  /*$verifica = $wpdb->get_results("
  47.                  SELECT id FROM $wpdb->posts, $wpdb->terms
  48.                  WHERE post_title = '$title'
  49.                  AND post_content = '$content'
  50.                  AND term_id = '$categorie'
  51.                  AND post_status = 'publish'  ");*/
  52.                  
  53.                   if( isset($_POST['submit-post']) ) { // if form has been submitted
  54.                  
  55.                   # some validation
  56.                       if(empty($title)) {
  57.                         echo "Please give your post a title<br />";
  58.                       }
  59.                       if (empty($content)){
  60.                         echo "Please write your post's content<br />";
  61.                       }
  62.                       if ($categorie == -1){
  63.                            echo "Please assign a category to your post.<br />";
  64.                       }
  65.                       if (empty($zombies)) {
  66.                             echo "Please input any number of Zombies";  
  67.                       }
  68.                       # if there are no similar posts ($verifica is empty) and user filled in the fields, insert post
  69.                       # also, redirect to the homepage to make sure we don't get 404-ed
  70.                       if (empty($verifica) && !empty($title) && !empty($content) && $categorie != -1) {
  71.                         $my_post = array(
  72.                          'post_title' => $title,
  73.                          'post_content' => $content,
  74.                          'post_status' => 'draft',
  75.                          'post_author' => 1,
  76.                          'post_category' => array($categorie),
  77.                           );
  78.                          
  79.                     $my_post = wp_insert_post($my_post);
  80.                     add_post_meta($my_post, 'Zombies', $zombies);
  81.  
  82.                          if ($_FILES) {
  83.                               foreach ($_FILES as $file => $array) {
  84.                                 $newupload = insert_attachment($file,$my_post);
  85.                                 // $newupload returns the attachment id of the file that
  86.                                 // was just uploaded. Do whatever you want with that now.
  87.                               }
  88.                             }
  89.                         // Now get all the attached images and set the first one as the post thumbnail
  90.                         $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $my_post . '&orderby=date&order=ASC' );
  91.  
  92.                         // If images exist for this page
  93.                         if($arrImages) {
  94.                      
  95.                             // Get array keys representing attached image numbers
  96.                             $arrKeys = array_keys($arrImages);                   
  97.                    
  98.                             // Get the first image attachment
  99.                             $iNum = $arrKeys[0];
  100.                             }
  101.                         set_post_thumbnail( $my_post, $iNum );
  102.                    
  103.                     // Finally, redirect the user somewhere like homepage, so we don't get 404-ed
  104.                     wp_redirect( home_url() );
  105.                       }
  106.                      
  107.                       # if $verifica is not empty, then we don't insert the post and we display a message
  108.                       else if( !empty($verifica) ) { echo "You are trying to submit the same post twice! Be nice.";  }
  109.                   }
  110.             ?>
  111.             <hr>
  112.             <form action="" enctype="multipart/form-data" method="post" name="myForm" class="jqtransform">
  113.                 <div class="rowElem">
  114.                 <label for="title">Title</label>
  115.                 <input type="text" name="title" id="title">
  116.                 </div>
  117.                 <div class="rowElem">
  118.                 <?php wp_dropdown_categories( 'show_option_none=Please choose a category&tab_index=4&taxonomy=category&hide_empty=0' ); ?>
  119.                 </div>
  120.                 <div class="rowElem">
  121.                 <label for="zombies">Zombies <small>(number; adds a custom field named 'Zombies' with the value you specify)</small></label>
  122.                 <input type="text" name="zombies" id="zombies">
  123.                 </div>
  124.                 <div class="rowElem">
  125.                 <label for="content">Post Content</label>
  126.                 <textarea name="content" id="content" cols="30" rows="10"></textarea>
  127.                 </div>
  128.                 <div class="rowElem">
  129.                 <input type="file" value="Upload" name="image-upload-2" id="image-upload-2">
  130.                 </div>
  131.                 <div class="rowElem">
  132.                 <input type="file" value="Upload" name="image-upload" id="image-upload">
  133.                 </div>
  134.                 <input type="submit" value="Insert post" name="submit-post">
  135.             </form>
  136.            
  137.            
  138.             <hr>
  139.            
  140.             </div><!-- #content -->
  141.         </div><!-- #container -->
  142.  
  143. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement