Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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);
- /*$verifica = $wpdb->get_results("
- SELECT id FROM $wpdb->posts, $wpdb->terms
- WHERE post_title = '$title'
- AND post_content = '$content'
- AND term_id = '$categorie'
- AND post_status = 'publish' ");*/
- if( isset($_POST['submit-post']) ) { // 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);
- if ($_FILES) {
- foreach ($_FILES as $file => $array) {
- $newupload = insert_attachment($file,$my_post);
- // $newupload returns the attachment id of the file that
- // was just uploaded. Do whatever you want with that now.
- }
- }
- // Now get all the attached images and set the first one as the post thumbnail
- $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $my_post . '&orderby=date&order=ASC' );
- // If images exist for this page
- if($arrImages) {
- // Get array keys representing attached image numbers
- $arrKeys = array_keys($arrImages);
- // Get the first image attachment
- $iNum = $arrKeys[0];
- }
- set_post_thumbnail( $my_post, $iNum );
- // Finally, redirect the user somewhere like homepage, so we don't get 404-ed
- 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="" enctype="multipart/form-data" method="post" name="myForm" class="jqtransform">
- <div class="rowElem">
- <label for="title">Title</label>
- <input type="text" name="title" id="title">
- </div>
- <div class="rowElem">
- <?php wp_dropdown_categories( 'show_option_none=Please choose a category&tab_index=4&taxonomy=category&hide_empty=0' ); ?>
- </div>
- <div class="rowElem">
- <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">
- </div>
- <div class="rowElem">
- <label for="content">Post Content</label>
- <textarea name="content" id="content" cols="30" rows="10"></textarea>
- </div>
- <div class="rowElem">
- <input type="file" value="Upload" name="image-upload-2" id="image-upload-2">
- </div>
- <div class="rowElem">
- <input type="file" value="Upload" name="image-upload" id="image-upload">
- </div>
- <input type="submit" value="Insert post" name="submit-post">
- </form>
- <hr>
- </div><!-- #content -->
- </div><!-- #container -->
- <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement