Opened 14 years ago
Closed 14 years ago
#18185 closed defect (bug) (fixed)
media_handle_sideload clears $title if $desc is not set
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 3.3 | Priority: | normal |
| Severity: | normal | Version: | 3.2.1 |
| Component: | Media | Keywords: | has-patch commit |
| Focuses: | Cc: |
Description
media_handle_sideload() -- found in wp-admin>includes>media.php -- is setting $title to an empty string if isset($desc) returns false.
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
$overrides = array('test_form'=>false);
$file = wp_handle_sideload($file_array, $overrides);
if ( isset($file['error']) )
return new WP_Error( 'upload_error', $file['error'] );
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = preg_replace('/\.[^.]+$/', '', basename($file));
$content = '';
// use image exif/iptc data for title and caption defaults if possible
if ( $image_meta = @wp_read_image_metadata($file) ) {
if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
$title = $image_meta['title'];
if ( trim( $image_meta['caption'] ) )
$content = $image_meta['caption'];
}
$title = isset($desc) ? $desc : '';
// Construct the attachment array
$attachment = array_merge( array(
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
'post_title' => $title,
'post_content' => $content,
), $post_data );
// Save the attachment metadata
$id = wp_insert_attachment($attachment, $file, $post_id);
if ( !is_wp_error($id) )
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
return $id;
}
Please note this line:
$title = isset($desc) ? $desc : '';
$desc is NULL by default. So if a value is not passed in the function call, $title will be set to an empty string.
It appears that it should be changed to this
$title = isset($desc) ? $desc : $title;
Attachments (1)
Change History (5)
Note: See
TracTickets for help on using
tickets.
Patch using if() for code readability