Advertisement
Guest User

Insert post from frontend, check for duplicates in wpdb

a guest
Feb 23rd, 2011
1,654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.04 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.                   if( 'POST' == $_SERVER['REQUEST_METHOD'] ) { // if form has been submitted
  47.                  
  48.                   # some validation
  49.                       if(empty($title)) {
  50.                         echo "Please give your post a title<br />";
  51.                       }
  52.                       if (empty($content)){
  53.                         echo "Please write your post's content<br />";
  54.                       }
  55.                       if ($categorie == -1){
  56.                            echo "Please assign a category to your post.<br />";
  57.                       }
  58.                       if (empty($zombies)) {
  59.                             echo "Please input any number of Zombies";  
  60.                       }
  61.                       # if there are no similar posts ($verifica is empty) and user filled in the fields, insert post
  62.                       # also, redirect to the homepage to make sure we don't get 404-ed
  63.                       if (empty($verifica) && !empty($title) && !empty($content) && $categorie != -1) {
  64.                         $my_post = array(
  65.                          'post_title' => $title,
  66.                          'post_content' => $content,
  67.                          'post_status' => 'draft',
  68.                          'post_author' => 1,
  69.                          'post_category' => array($categorie),
  70.                           );
  71.                          
  72.                     $my_post = wp_insert_post($my_post);
  73.                     add_post_meta($my_post, 'Zombies', $zombies);
  74.                     wp_redirect( home_url() );
  75.                       }
  76.                      
  77.                       # if $verifica is not empty, then we don't insert the post and we display a message
  78.                       else if( !empty($verifica) ) { echo "You are trying to submit the same post twice! Be nice.";  }
  79.                   }
  80.             ?>
  81.             <hr>
  82.             <form action="" method="post" name="myForm">
  83.                 <p><label for="title">Title</label>
  84.                 <input type="text" name="title" id="title">
  85.                 </p>
  86.                 <p>
  87.                 <?php wp_dropdown_categories( 'show_option_none=Please choose a category&tab_index=4&taxonomy=category&hide_empty=0' ); ?>
  88.                 </p>
  89.                 <p>
  90.                 <label for="zombies">Zombies <small>(number; adds a custom field named 'Zombies' with the value you specify)</small></label>
  91.                 <input type="text" name="zombies" id="zombies">
  92.                 </p>
  93.                 <p>
  94.                 <label for="content">Post Content</label>
  95.                 <textarea name="content" id="content" cols="30" rows="10"></textarea>
  96.                 </p>
  97.                 <input type="submit" value="Insert post">
  98. <?php wp_nonce_field( 'myForm' ); ?>
  99.  
  100.             </form>
  101.            
  102.             <hr>
  103.            
  104.             </div><!-- #content -->
  105.         </div><!-- #container -->
  106.  
  107. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement