Is there a function that allows me to change the filename of an attachment, based on the Attachment ID I have?
Thanks!
Dennis
Is there a function that allows me to change the filename of an attachment, based on the Attachment ID I have?
Thanks!
Dennis
This will allow you to rename an attachment as soon as its uploaded:
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
$newfilename = "NEW FILE NAME HERE";
$newfile = $path['dirname']."https://wordpress.stackexchange.com/".$newfilename.".".$path['extension'];
rename($file, $newfile);
update_attached_file( $post_ID, $newfile );
}