Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to change the way Wordpress rename uploaded files on upload. As I upload an image whose filename is "see-sunset.jpg", Wordpress will set up scaled versions and rename filenames to different combinations of "/2013/11/see-sunset-{WIDTHxHEIGHT}.jpg" (dipending on thumbnail sizes).

Is it possible to add a function or a filter to change "see-sunset" for "{POST-ID}" so that the final URL (on uploading) is going to be "/2013/11/{POST-ID}-{WIDTHxHEIGHT}.jpg" or better one "/2013/11/{POST-ID}{WIDTHxHEIGHT}.jpg"

Renaming uploads filenames to POST-ID, is it, in your opinion, convenient or not considering how WordPress works ?

Many Thanks

EDIT BECAUSE OF SIMILAR QUESTION

Thank you, ialocin !

Tried as suggested here Change attachment filename.

It seems working, but not call the post-ID, instead the attachment-ID (I guess). I mean I am trying to make /2013/11/{POST-ID}.jpg having the same id as the post URL myblog.com/category/post-ID/

Is there a way for the attachment of a post to be named as the post URL, having permalink set to post-ID (/%post_id%/) ?

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 );

}

EDIT BECAUSE TRIED IT AGAIN AND DOES NOT WORK PROPERLY

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

Could someone try this out in order to confirm (me using latest WP version).

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 );

}
share|improve this question
    
possible duplicate of Change attachment filename –  ialocin Nov 16 '13 at 17:55
    
What sould be the name of file when there are many attachments for one post? –  Krzysiek Dróżdż Nov 16 '13 at 21:43
    
Hi Krzysiek, thank you for commenting. Users are not allowed to post more than 1 attachment for post in the theme I am working out. Could you please have a look on the edit (tried out as suggested in similar question). Greetings. –  morelo Nov 16 '13 at 22:00
    
¿ Could someone help ? This functions is not working properly, featured image is not being showed in admin panel, neither it is as thumbnails, just works for "full" size, not for thumbnails. Could someone please try this out ? Thank you. –  morelo Nov 18 '13 at 18:37
    
I don't want to dishearten you, but this seems overall not like the best approach. For one attachment aren't always attached, besides that, what happens if you attach the same image to more then one post. There are a couple more point that aren't optimal. Last but not least, does it really make sense to have this? –  ialocin Nov 20 '13 at 23:04
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.