Advertisement
Guest User

file upload

a guest
Jun 12th, 2016
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.67 KB | None | 0 0
  1. function brush_packs() {
  2.   $labels = array(
  3.     'name'               => _x( 'Brush Packs', 'post type general name' ),
  4.     'singular_name'      => _x( 'Brush Pack', 'post type singular name' ),
  5.     'add_new'            => _x( 'Add New', 'brushes' ),
  6.     'add_new_item'       => __( 'Add New Brush Pack' ),
  7.     'edit_item'          => __( 'Edit Brush Pack' ),
  8.     'new_item'           => __( 'New Brush Pack' ),
  9.     'all_items'          => __( 'All Brushes' ),
  10.     'view_item'          => __( 'View Brushes' ),
  11.     'search_items'       => __( 'Search Brushes' ),
  12.     'not_found'          => __( 'No Brushes found' ),
  13.     'not_found_in_trash' => __( 'No Brushes found in the Trash' ),
  14.     'parent_item_colon'  => '',
  15.     'menu_name'          => 'Brushes'
  16.   );
  17.   $args = array(
  18.     'labels'        => $labels,
  19.     'description'   => 'text',
  20.     'public'        => true,
  21.     'menu_position' => 5,
  22.     'menu_icon' => 'dashicons-admin-customizer',
  23.     'supports'      => array( 'title', 'thumbnail'),
  24.     'has_archive'   => true,
  25.   );
  26.   register_post_type( 'brushes', $args );
  27. }
  28. add_action( 'init', 'brush_packs' );
  29.  
  30. function brushes_taxonomy() {  
  31.     register_taxonomy(  
  32.         'brushes_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
  33.         'brushes',        //post type name
  34.         array(  
  35.             'hierarchical' => true,  
  36.             'label' => 'Brush Types',  //Display name
  37.             'query_var' => true,
  38.             'rewrite' => array(
  39.                 'slug' => 'brushes', // This controls the base slug that will display before each term
  40.                 'with_front' => false // Don't display the category base before
  41.             )
  42.         )  
  43.     );  
  44. }  
  45. add_action( 'init', 'brushes_taxonomy');
  46.  
  47.  
  48. function add_custom_meta_boxes() {
  49.  
  50.     // Define the custom attachment for posts
  51.     add_meta_box(
  52.         'brush_pack_attachment',
  53.         'Custom Attachment',
  54.         'brush_pack_attachment',
  55.         'brushes',
  56.         'normal'
  57.     );
  58.  
  59. } // end add_custom_meta_boxes
  60. add_action('add_meta_boxes', 'add_custom_meta_boxes');
  61.  
  62. function brush_pack_attachment() {
  63.  
  64.     wp_nonce_field(plugin_basename(__FILE__), 'brush_pack_attachment_nonce');
  65.      
  66.     $html = '<p class="description">';
  67.         $html .= 'Upload your PDF here.';
  68.     $html .= '</p>';
  69.     $html .= '<input type="file" id="brush_pack_attachment" name="brush_pack_attachment" value="" size="25" />';
  70.      
  71.     echo $html;
  72.  
  73. } // end brush_pack_attachment
  74.  
  75. function save_custom_meta_data($id) {
  76.  
  77.     /* --- security verification --- */
  78.     if(!wp_verify_nonce($_POST['brush_pack_attachment_nonce'], plugin_basename(__FILE__))) {
  79.       return $id;
  80.     } // end if
  81.        
  82.     if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  83.       return $id;
  84.     } // end if
  85.        
  86.     if('brushes' == $_POST['post_type']) {
  87.       if(!current_user_can('edit_page', $id)) {
  88.         return $id;
  89.       } // end if
  90.     } else {
  91.         if(!current_user_can('edit_page', $id)) {
  92.             return $id;
  93.         } // end if
  94.     } // end if
  95.     /* - end security verification - */
  96.      
  97.     // Make sure the file array isn't empty
  98.     if(!empty($_FILES['brush_pack_attachment']['name'])) {
  99.          
  100.         // Setup the array of supported file types. In this case, it's just PDF.
  101.         $supported_types = array('application/pdf');
  102.          
  103.         // Get the file type of the upload
  104.         $arr_file_type = wp_check_filetype(basename($_FILES['brush_pack_attachment']['name']));
  105.         $uploaded_type = $arr_file_type['type'];
  106.          
  107.         // Check if the type is supported. If not, throw an error.
  108.         if(in_array($uploaded_type, $supported_types)) {
  109.  
  110.             // Use the WordPress API to upload the file
  111.             $upload = wp_upload_bits($_FILES['brush_pack_attachment']['name'], null, file_get_contents($_FILES['brush_pack_attachment']['tmp_name']));
  112.      
  113.             if(isset($upload['error']) && $upload['error'] != 0) {
  114.                 wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
  115.             } else {
  116.                 add_post_meta($id, 'brush_pack_attachment', $upload);
  117.                 update_post_meta($id, 'brush_pack_attachment', $upload);    
  118.             } // end if/else
  119.  
  120.         } else {
  121.             wp_die("The file type that you've uploaded is not a PDF.");
  122.         } // end if/else
  123.          
  124.     } // end if
  125.      
  126. } // end save_custom_meta_data
  127. add_action('save_post', 'save_custom_meta_data');
  128.  
  129. function update_edit_form() {
  130.     echo ' enctype="multipart/form-data"';
  131. } // end update_edit_form
  132. add_action('post_edit_form_tag', 'update_edit_form');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement