1

I have been trying this code out (based on this Change attachment filename ), it does not work properly. For instance, featured image is not being displayed in admin panel single post, neither are being created and uploaded all WordPress re-scaled sizes (as in media settings).

Could someone try this out in order to confirm (me using latest WP version) or could someone give a fix ?

Thank you !

add_action('add_attachment', 'rename_attacment');
function rename_attacment($post_ID){

    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename

    $newfilename = "{$post_ID}";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.