• narkanister

    (@narkanister)


    Hi
    I followed the excellent tutorial on creating metaboxes here,
    but having tried many times, I have the same problem:
    the $_FILES[‘mp3_attachment’][‘name’] is always empty.

    Can’t figure out why. Help appreciated ??

    The code is below:

    ‘talk’ is a custom post type.

    // Add custom upload field for the mp3s
    function add_custom_meta_boxes() {
    
    	// Define the custom mp3 attachment for Talk posts
    	add_meta_box(
    		'mp3_attachment',
    		'MP3 File',
    		'mp3_attachment',
    		'talk',
    		'normal'
    	);
    } // end add_custom_meta_boxes
    add_action('add_meta_boxes', 'add_custom_meta_boxes');
    
    // Create mp3 upload form
    function mp3_attachment() {
    
    	wp_nonce_field(plugin_basename(__FILE__), 'mp3_custom_attachment_nonce');
    
    	$html = '<p class="description">';
    	$html .= 'Upload your mp3 file here.';
    	$html .= '</p>';
    	$html .= '<input type="file" id="mp3_attachment" name="mp3_attachment" value="" />';
    
    	echo $html;
    } // end wp_custom_attachment
    
    // Save mp3 upload to database
    function save_mp3_custom_meta_data($id) {
    
    	/* --- security verification --- */
    	if(!wp_verify_nonce($_POST['mp3_custom_attachment_nonce'], plugin_basename(__FILE__))) {
    		return $id;
    	} // end if
    
    	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    		return $id;
    	} // end if
    
    	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;
    		} // end if
    	} // end if
    	/* - end security verification - */
    
    	// Make sure the file array isn't empty
    	if(!empty($_FILES['mp3_attachment']['name'])) {
    
    		// Setup the array of supported file types. In this case, it's just PDF.
    		$supported_types = array('audio/mpeg');
    
    		// Get the file type of the upload
    		$arr_file_type = wp_check_filetype(basename($_FILES['mp3_attachment']['name']));
    		$uploaded_type = $arr_file_type['type'];
    
    		// Check if the type is supported. If not, throw an error.
    		if(in_array($uploaded_type, $supported_types)) {
    
    			// Use the WordPress API to upload the file
    			$upload = wp_upload_bits($_FILES['mp3_attachment']['name'], null, file_get_contents($_FILES['mp3_attachment']['tmp_name']));
    
    			if(isset($upload['error']) && $upload['error'] != 0) {
    				wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
    			} else {
    				add_post_meta($id, 'mp3_attachment', $upload);
    				update_post_meta($id, 'mp3_attachment', $upload);
    			} // end if/else
    
    		} else {
    			wp_die("The file type that you've uploaded is not an mp3.");
    		} // end if/else
    
    	} // end if
    
    } // end save_custom_meta_data
    add_action('save_post', 'save_mp3_custom_meta_data');
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘mp3 upload metabox not working’ is closed to new replies.