Make WordPress Core

Opened 5 weeks ago

Last modified 5 days ago

#63509 new enhancement

Use proper filename for attachment post title

Reported by: rilwis's profile rilwis Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords: has-patch
Focuses: Cc:

Description

In media_handle_upload() function, the file name (and thus, attachment title) is gotten from $_FILES[ $file_id ]['name']. This is the raw/original file name when uploaded. However, developers can change file name after upload by filtering to sanitize_file_name or use unique_filename_callback parameter. This makes the file name (and title) doesn't represent the correct file name after upload.

$name = $_FILES[ $file_id ]['name'];
$ext  = pathinfo( $name, PATHINFO_EXTENSION );
$name = wp_basename( $name, ".$ext" );

$url     = $file['url'];
$type    = $file['type'];
$file    = $file['file'];
$title   = sanitize_text_field( $name );

A simple patch for this is this:

$name = $file['file']; // Get the proper name
$ext  = pathinfo( $name, PATHINFO_EXTENSION );
$name = wp_basename( $name, ".$ext" );

I tested the patch with classic editor and it works well. However, I haven't figured out for the block editor.

Attachments (1)

63509.diff (739 bytes) - added by rilwis 5 weeks ago.
Patch for use proper file name after upload

Download all attachments as: .zip

Change History (2)

@rilwis
5 weeks ago

Patch for use proper file name after upload

#1 @Presskopp
5 days ago

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.