• I am setting up a photo directory, I would need photo URLs to be absolutely clean and not dependent upon users changing their post title. I believe ATTACH-ID to be a reliable data, much more reliable than others in order to grant a lifelong URL.

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

    Would be possible to add a function or a filter to change “see-sunset” for “{ATTACH-ID}” so that the final URL (on uploading) is going to be “/2013/11/{ATTACH-ID}.jpg” for the main image and “/2013/11/{ATTACH-ID}{WIDTHxHEIGHT}.jpg” for scaled ones.

    Thank you

Viewing 1 replies (of 1 total)
  • Hello,

    I found this code on the net, how can this be changed to rename the files to attachment ID?

    Thanks

    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 = “NEW FILE NAME HERE”;
    $newfile = $path[‘dirname’].”/”.$newfilename.”.”.$path[‘extension’];

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

    }

Viewing 1 replies (of 1 total)

The topic ‘Rename attachment filenames to "attachment ID" on upload’ is closed to new replies.