The effects of alcohol on
sustanon 250 leucine for β real weight loss & bodybuilding benefits?
Limited Support
Our support team is currently on holiday from December 25, 2025 to January 7, 2026, and replies may be delayed during this period.
We appreciate your patience and understanding while our team is away. Thank you for being part of the wpDiscuz community!
Merry Christmas and Happy Holidays! π
Question [Solved] Rename a file during upload with the post title
How-to and Troubleshooting
(@benn)
Active Member
Joined: 3 years ago
Hi,
I would like to rename the attached files of a comment (using the media uploader plugin) and use the title of the current post. So I created a snippet to add this code when the sanitize_file_name function is call.
But this doesn't work, it seems that I can't acces to the post, even if I use a global $post.
Do you have an idea ?
Thanks
Β
function my_custom_file_name( $filename ) {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
if ( isset( $_REQUEST['post_id'] ) && is_numeric( $_REQUEST['post_id'] ) ) {
$postObj = get_post( $_REQUEST['post_id'] );
$postSlug = sanitize_title( $postObj->post_title );
}
if ( isset( $postSlug ) && ! empty( $postSlug ) ) {
$finalFileName = $postSlug;
} // File name will be the same as the post slug.
else {
$finalFileName = sanitize_title( $name );
} // File name will be the same as the image file name, but sanitized.
return $finalFileName . $ext;
}
add_filter( 'sanitize_file_name', 'my_custom_file_name', 100 );
(@benn)
Active Member
Joined: 3 years ago
New test : when I try to get the url of my page, I only get the base url of my domain, not the full url of my post...
Is it because the upload is done in js ?
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
(@asti)
Joined: 8 years ago
Illustrious Member
Posts: 8204
Jun 19, 2023 8:08 am
@benn,
The wp_insert_attachment function is used for inserting the attachments. The wp_insert_attachment calls the wp_insert_post function as well. So you should use those functions.Β
As far as the file uploading use the AJAX method you can't change the filename using the sanitize_file_name. The global post isn't accessible when the AJAX method is used for file uploading.Β
(@benn)
Joined: 3 years ago
Active Member
Posts: 3
Jun 19, 2023 9:34 pm
@asti Thank you for your answer and your explanation !
I finally use the add_attachment function and it works well !
Here is my code, if it could helps somebody.
add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
Β Β $file = get_attached_file($post_ID);
Β Β $path = pathinfo($file);
Β Β Β Β //dirnameΒ Β = File Path
Β Β Β Β //basenameΒ = Filename.Extension
Β Β Β Β //extension = Extension
Β Β Β Β //filenameΒ = Filename
$post_title = get_the_title(get_post_parent($post_ID));
$user = wp_get_current_user()->display_name;
$newfilename = $user.'-'.$post_title;
$newfile = $path['dirname'].'/myFolder/'.$newfilename.'.'.$path['extension'];
Β Β // Update WordpressΒ
Β Β rename($file, $newfile);Β Β Β
Β Β update_attached_file( $post_ID, $newfile );
// Update metadata
$pathNewFile = pathinfo($newfile);
$meta = wp_get_attachment_metadata($post_ID);
$meta['file'] = str_replace( $path['basename'], $pathNewFile['basename'], $meta['file'] );
wp_update_attachment_metadata( $post_ID, $meta );
// Update media library post title
Β Β $post = get_post($post_ID);Β
Β Β $post->post_title = $newfilename;
Β Β wp_update_post( $post );
}
(@asti)
Joined: 8 years ago
Illustrious Member
Posts: 8204
Jun 20, 2023 7:13 am
Thank you for sharing the information. We really appreciate it.
We're going to mark this topic as solved and close it.
Feel free to open a new topic if you have any other questions.