Advertisement
Guest User

Untitled

a guest
Oct 25th, 2010
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. <?php
  2. /**
  3.  * post-process.php
  4.  * make sure to include post-process.php in your functions.php. Use this in functions.php:
  5.  *
  6.  * get_template_part('post-process');
  7.  *
  8.  */
  9. function do_insert() { 
  10.     if( 'POST' == $_SERVER['REQUEST_METHOD']
  11.         && !empty( $_POST['action'] )
  12.         && $_POST['post_type'] == 'book' ) { // Check what the post type is here instead
  13.        
  14.         // Setting the 'post_type' => $_POST['post_type'] in the $post array below causes 404
  15.         // Just set it based on what is set in the above IF $_POST type == 'book'.
  16.         // and below do 'post_type' => 'book'
  17.    
  18.         // Do some minor form validation to make sure there is content
  19.         if (isset ($_POST['title'])) { $title =  $_POST['title']; } else { echo 'Please enter a title'; }
  20.         if (isset ($_POST['description'])) { $description = $_POST['description']; } else { echo 'Please enter the content'; }
  21.        
  22.         $tags = trim( $_POST['post_tags'] );
  23.         // Get the array of selected categories as multiple cats can be selected
  24.         $cat = array( $_POST['cat'] );
  25.        
  26.         // Add the content of the form to $post as an array
  27.         $post = array(
  28.             'post_title'    => $title,
  29.             'post_content'  => $description,
  30.             'post_category' => $cat, // Usable for custom taxonomies too
  31.             'tags_input'    => $tags,
  32.             'post_status'   => 'publish', // Choose: publish, preview, future, etc.
  33.             'post_type'     => 'book' // Set the post type based on the IF is post_type X
  34.         );
  35.         wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function
  36.                                 // http://codex.wordpress.org/Function_Reference/wp_insert_post
  37.     } // end IF
  38. }
  39.  
  40. // Do the wp_insert_post action to insert it
  41. do_action('wp_insert_post', 'do_insert');
  42. ?>
  43.  
  44.  
  45. <!-- Put this in your theme template file -->
  46. <div id="postbox">
  47.    
  48.     <form id="new_post" name="new_post" method="post" action="<?php bloginfo('template_directory');?>/post-process.php">
  49.    
  50.         <p><label for="title">Title</label><br />
  51.         <input type="text" id="title" value="" tabindex="1" size="20" name="title" /></p>
  52.        
  53.         <p><label for="description">Description</label><br />
  54.         <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
  55.         </p>
  56.        
  57.         <p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>
  58.        
  59.         <p><label for="post_tags">Tags</label>
  60.         <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
  61.        
  62.         <p><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
  63.        
  64.         <input type="hidden" name="post_type" id="post_type" value="book" />
  65.         <input type="hidden" name="action" value="post" />
  66.         <?php wp_nonce_field( 'new-post' ); ?>
  67.    
  68.     </form>
  69.  
  70. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement