Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type and a metabox with a file input.

I can insert the attachment, but I can't update the attachment metadata and I don't know how to fix it because I don't receive any error.

Here is my code:

$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id,  $attach_data ); 

echo $attach_id;
echo '<pre>';
print_r($filename);
echo '</pre>';
echo '<pre>';
print_r($attach_data);
echo '</pre>';

and here is the output:

96
Array
(
    [name] => one.png
    [type] => image/png
    [tmp_name] => /tmp/phphQ0e2v
    [error] => 0
    [size] => 144555
)
Array
(
)

As you can see, $attach_data is empty :(

share|improve this question
1  
Solved. I had to add $upload = wp_handle_upload($filename, array('test_form' => false)); and then pass "$upload['file']" to wp_insert_attachment and wp_update_attachment_metadata instead of $filename :) –  Klian Apr 24 '12 at 17:20
1  
Please add your solution as an answer and mark the question as answered. Thanks. –  toscho Apr 25 '12 at 15:31
    
It is important to note that the first argument of wp_handle_upload is not a string but an array. So just passing the file-path won't work.. –  hugo der hungrige Apr 10 at 19:41

2 Answers 2

From comment:

Let WordPress generate a file path and use that for the next steps:

$upload    = wp_handle_upload($filename, array('test_form' => false));
$attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id );
wp_update_attachment_metadata( $attach_id,  $attach_data ); 
share|improve this answer

This is, what finally fixed it for me:

apply_filters('wp_handle_upload', array(
    'file' => $file_path, 
    'url' => $file_url, 
    'type' => $file_type), 
'upload');

Explanation: I'm not quite sure why this fixed the error for me, but I assume that this either has something to do with plugins using the wp_handle_upload hook or that the filters add meta-data to the attachment, which otherwise would be missing in the wp_generate_attachment_metadata function.

Full function:

function add_to_media_lib($file_url, $file_path, $parent_post_id)
{
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');

// Check the type of tile. We'll use this as the 'post_mime_type'.
$file_type = wp_check_filetype(basename($file_url), null);

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

// Prepare an array of post data for the attachment.
$attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename($file_url),
    'post_mime_type' => $file_type['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_url)),
    'post_content' => '',
    'post_status' => 'inherit',
    'post_parent' => $parent_post_id
);

// Insert the attachment.
$attach_id = wp_insert_attachment($attachment, $file_url, $parent_post_id);

// apply filters (important in some environments)
apply_filters('wp_handle_upload', array('file' => $file_path, 'url' => $file_url, 'type' => $file_type), 'upload');


// Generate the metadata for the attachment, and update the database record.
if ($attach_data = wp_generate_attachment_metadata($attach_id, $file_path)) {
    wp_update_attachment_metadata($attach_id, $attach_data);
} else {
    echo '<div id="message" class="error"><h1>Failed to create PDF-thumbnail Meta-Data</h1><pre>' . print_r($attach_data) . '</pre></div>';
}

return $attach_id;
}
share|improve this answer
    
Always add a short description to why your solution works, don't just add a lot of code and says it works. –  Pieter Goosen Apr 10 at 21:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.