Wednesday, 5 November 2014

how to upload multiple file in wodpress in Meta Box

In Function.Php



//Add Metabox For Floor Plan
    add_action('post_edit_form_tag', 'update_edit_form');

    function update_edit_form() {
      echo ' enctype="multipart/form-data"';
      } // end update_edit_form

    add_action('add_meta_boxes', 'the_upload_metabox');
  
    function the_upload_metabox() { 
        // Define the custom attachment for posts 
        add_meta_box( 
            'wp_custom_attachment', 
            'Upload File', 
            'wp_custom_file_attachment','property'   
        );  
    }

    // The custom file attachment function
    function wp_custom_file_attachment() { 
     
      global $post;
      $theFILE=  get_post_meta($post->ID,'wp_custom_attachment',true);
     // $theFILE[0]['url']
   
     global $wpdb;

$media_file = get_post_meta( $post->ID, $key = '_wp_attached_file', true );
if(!empty($media_file)) { $theFILE = $media_file; }


      wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce'); 
      $html = '<p class="description">';
      $html .= 'Upload the File.';
      if(count($theFILE)>0 && is_array($theFILE)){
      $html.="Uploaded File:";

 foreach($theFILE as $sdds){

      ?>
       <img src="<?php echo $sdds['url']; ?>" width="50" height="50"  />
       <?php
   
       }
      }

$html .= '</p>';
$html .= '<input id="wp_custom_attachment" title="select file" multiple="multiple" name="wp_custom_attachment[]" size="25" type="file" value="" />'; 

    echo $html;
     
    }
     add_action('save_post', 'save_custom_meta_data');
    // Saving the uploaded file details
    function save_custom_meta_data($id) { 
        /* --- security verification --- */ 
        if(isset($_POST['wp_custom_attachment_nonce']))
        if(!wp_verify_nonce($_POST['wp_custom_attachment_nonce'], plugin_basename(__FILE__))) { 
          return $id; 
        } // end if 
           
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
          return $id; 
        } // end if 
         if(isset($_POST['post_type']))  
        if('page' == $_POST['post_type']) { 
          if(!current_user_can('edit_page', $id)) { 
            return $id; 
          } // end if 
        } else { 
            if(!current_user_can('edit_page', $id)) { 
                return $id; 
            }
        }
          
        // Make sure the file array isn't empty 
        if(!empty($_FILES['wp_custom_attachment']['name'])) {                        
            // Get the file type of the upload 
             $flag=0;
            for($i=0;$i<count($_FILES['wp_custom_attachment']['name']);$i++){
                if(!empty($_FILES['wp_custom_attachment']['name'][$i])){
                    $flag=1;
                // Use the WordPress API to upload the multiple files
                $upload[] = wp_upload_bits($_FILES['wp_custom_attachment']['name'][$i], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name'][$i])); 
                }
                }
                if($flag==1)
                update_post_meta($id, 'wp_custom_attachment', $upload);      
            
        }
         
    }

/************ Call On Display Page ***********/

<?php
        $imagess = get_post_meta($post->ID, 'uniplan_floorplan', false );
        foreach($imagess as $imgrow)
        {
        $imgurl=wp_get_attachment_url($imgrow);
        ?>
        <li><a href="<?php echo $imgurl; ?>" data-litebox-group="galone" class="litebox">
        <img class="a1" src="<?php echo $imgurl; ?>"alt="<?php echo $posts->post_title; ?>"></a></li>
        <?php
        } ?> 

No comments :

Post a Comment