Advertisement
olie480

WP: Insert a file from form into wordpress media library

Dec 21st, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. //**************************************
  2. // Attachment Insert Form
  3. //**************************************
  4. function insert_attachment_form($postID) {
  5. ?>
  6.     <form id="file-form" name="file-form" method="POST" action="" enctype="multipart/form-data" >
  7.         <input type="file" id="async-upload" name="async-upload" />
  8.         <input type="hidden" name="postID" value="<?php echo $postID; ?>" />
  9.         <?php wp_nonce_field('client-file-upload', 'client-file-upload'); ?>
  10.         <input type="submit" value="Upload" id="submit" name="submit" />
  11.     </form>
  12. <?php }
  13.  
  14. //**************************************
  15. // Process Attachment Form
  16. //**************************************
  17. function process_attachment() {
  18.     // verify this came from the our screen and with proper authorization,
  19.     // because save_post can be triggered at other times
  20.     if ( !wp_verify_nonce( $_POST['client-file-upload'], 'client-file-upload') ) {
  21.         return $post->ID;
  22.     }
  23.  
  24.     // Is the user allowed to edit the post or page?
  25.     if ( !current_user_can( 'publish_posts', $post->ID ))
  26.         return $post->ID;
  27.  
  28.     if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_FILES )) {
  29.         require_once(ABSPATH . 'wp-admin/includes/admin.php');
  30.         $id = media_handle_upload('async-upload', $_POST['postID']);
  31.         unset($_FILES);
  32.     }
  33. }
  34.  
  35. add_action('init', 'process_attachment');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement