<?php
/**
* Template Name: Post Insert
*
* A custom page template that allows users to submit a post from the front end.
* Based on the Twenty Ten page template.
* The "Template Name:" bit above allows this to be selectable
* from a dropdown menu on the edit page screen.
*
* @author iCosmin (http://icosmin.com)
* @package WordPress
* @subpackage Twenty_Ten
* @since Twenty Ten 1.0
*/
get_header(); ?>
<div id="container" class="one-column">
<div id="content" role="main">
<?php
global $wpdb;
global $post;
$title = $_POST['title']; // get the inputted title
$content = $_POST['content']; // get the inputted content
$categorie = $_POST['cat']; // get the category selected by user
$zombies = $_POST['zombies'];
# run a query to check for a post containing the data that our user is about to submit
# store results in $verifica
$sql = "
SELECT wposts.*
FROM $wpdb->posts wposts
LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE wposts.post_status = 'publish'
AND wposts.post_title = '$title'
AND wposts.post_content = '$content'
AND wpostmeta.meta_key = 'Zombies'
AND wpostmeta.meta_value = '$zombies'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN($categorie)";
$verifica = $wpdb->get_results($sql);
if( 'POST' == $_SERVER['REQUEST_METHOD'] ) { // if form has been submitted
# some validation
if(empty($title)) {
echo "Please give your post a title<br />";
}
if (empty($content)){
echo "Please write your post's content<br />";
}
if ($categorie == -1){
echo "Please assign a category to your post.<br />";
}
if (empty($zombies)) {
echo "Please input any number of Zombies";
}
# if there are no similar posts ($verifica is empty) and user filled in the fields, insert post
# also, redirect to the homepage to make sure we don't get 404-ed
if (empty($verifica) && !empty($title) && !empty($content) && $categorie != -1) {
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'draft',
'post_author' => 1,
'post_category' => array($categorie),
);
$my_post = wp_insert_post($my_post);
add_post_meta($my_post, 'Zombies', $zombies);
wp_redirect( home_url() );
}
# if $verifica is not empty, then we don't insert the post and we display a message
else if( !empty($verifica) ) { echo "You are trying to submit the same post twice! Be nice."; }
}
?>
<hr>
<form action="" method="post" name="myForm">
<p><label for="title">Title</label>
<input type="text" name="title" id="title">
</p>
<p>
<?php wp_dropdown_categories( 'show_option_none=Please choose a category&tab_index=4&taxonomy=category&hide_empty=0' ); ?>
</p>
<p>
<label for="zombies">Zombies <small>(number; adds a custom field named 'Zombies' with the value you specify)</small></label>
<input type="text" name="zombies" id="zombies">
</p>
<p>
<label for="content">Post Content</label>
<textarea name="content" id="content" cols="30" rows="10"></textarea>
</p>
<input type="submit" value="Insert post">
<?php wp_nonce_field( 'myForm' ); ?>
</form>
<hr>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>