• Hi

    I would like to use a meta box to add a file to a post and use it as an attachment but I can’t seem to figure it out.

    Currently I’m using this code:

    function save_post_bijlage($post_id, $post = null)
    {
    	global $post;
    	$filename = $_FILES['bijlage'];
    	//echo "1:" . $_FILES['bijlage'];
    	print_r($filename);
    
    	$wp_filetype = wp_check_filetype(basename($filename), null );
    	$attachment = array(
    						 'post_mime_type' => $wp_filetype['type'],
    						 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
    						 'post_content' => '',
    						 'post_status' => 'inherit'
    	);
    	$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
    	// you must first include the image.php file
    	// for the function wp_generate_attachment_metadata() to work
    	require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    	$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    	wp_update_attachment_metadata( $attach_id,  $attach_data ); 
    
    }
    
    add_action('save_post', 'save_post_bijlage');

    and:

    add_action(‘admin_menu’, ‘publicatie_meta’);
    function publicatie_meta() {
    add_meta_box(“publicatie_bijlage”, “Bijlage”, “meta_options”, “publicatie”, “side”, “low”);
    }

    function meta_options() { ?>
    	<input type="file" name="bijlage" id="bijlage"/>
    <?php } ?>

    I can’t seem to figure out the link between getting the file uploaded to the server and attaching the attachment to the post.

    I hope someone can help me out.

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘File upload with Custom Post Type’ is closed to new replies.