[wp-trac] [WordPress Trac] #63509: Use proper filename for attachment post title
WordPress Trac
noreply at wordpress.org
Mon Jun 2 07:57:53 UTC 2025
#63509: Use proper filename for attachment post title
-------------------------+-----------------------------
Reporter: rilwis | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version:
Severity: normal | Keywords:
Focuses: |
-------------------------+-----------------------------
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.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/63509>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list