Support » Fixing WordPress » Problem WP automatically change image filenames after upload

  • dragongate

    (@dragongate)


    Hello, I’m encountering a problem with WP, whenever I upload an image file to my media library, WP automatically adds “-1” to the end of the filename of some image files even there is no duplicates in my media library!!

    How can I fix this problem? I would like to retain the original filename.

    Pls. help me.
    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Sundar

    (@phptechie)

    Hi,

    1. Please install new WordPress setup / update to WP 5.5.1 & then test the file uploads.

    2. Deactivate all plugins and check the upload functionality.

    You could check more details here

    https://core.trac.wordpress.org/ticket/48974

    Thanks,

    Thread Starter dragongate

    (@dragongate)

    Hello Sundar, yes I have latest WP 5.5.1 installed and it is not a matter caused by a plugin, as I read from the link you posted here that this is a new feature added by WP i is not caused by a plugin conflict.

    They suggested that “But if you need to override that behavior, since WP 4.5.0 there has been the wp_unique_filename filter which you can use in a custom plugin.”

    but I don’t know how to use this code in a plugin and I don’t know how to make a plugin!

    ca you help show me how?

    Pls. help thx

    Sundar

    (@phptechie)

    Hi,

    You can create plugin by override that behavior using the following code.

    To create a custom plugin, all you need to do is create a folder and then create a single file with lines of content. Navigate to the wp-content/plugins folder, and create a new folder named “customuniquefile”. Inside this new folder, create a file named uniquefilename.php. Open the file in a text editor, and paste the following code & save it:

    <?php
    /*
       Plugin Name: Custom Unique File Name
       Plugin URI: --
       description: A plugin to keep actual image filenames after upload
       Version: 1.0
       License: GPL2
     */
    
    add_filter( 'wp_unique_filename', 'my_ununique_filename', 10, 4 );
    
    function my_ununique_filename( $filename, $ext, $dir, $unique_filename_callback ) {
            $esc_ext = preg_quote( $ext );
    
            if ( 1 === preg_match( "/\d+x\d+-\d{$esc_ext}\$/", $filename ) ) {
                    if ( ! file_exists( "{$dir}/{$filename}" ) ) {
                            $filename = preg_replace( "/-\d{$esc_ext}\$/", $ext, $filename );
                    }
            }
    
            return $filename;
    } ?>

    After that, you can go into the WordPress admin back end to activate your plugin.

    Thanks!

    Thread Starter dragongate

    (@dragongate)

    Hello Sundar,
    I did exactly what you told me here above, and I activated the plugin in my admin dashboard then uploaded the images but unfortunately still have same problem the images name change automatically and adds “-1” , “-2” , “-3″… etc. to the end of the filename of some image files every time I delete the images from my media library then re-upload the number increase by (1) like for example if I uploaded image name (x) first time then deleted it, when I try to upload same image again with name (s), WP change automatically the name to (x-1), then if I deleted the image (x-1) and try to upload the original image (x) again, the second time I upload the name change automatically to (x-2), then if I repeat third time the name change to (x-3) even though I am deleting the image from my media library!!

    Pls. help me if you can,
    Thank you

    Thread Starter dragongate

    (@dragongate)

    Sorry, typing mistake in line-8
    Mistake: with name (s), WP change automatically…
    Correction: with name (x), WP change automatically

    Sundar

    (@phptechie)

    Hello,

    I have checked file uploads in different local environment WP setup and it works fine ( I don’t have file name issues ).

    Please take a backup copy, reset your WordPress setup using https://wordpress.org/plugins/wp-reset/ plugin and then check the file uploads.

    Also you can try rename options & features for media uploads
    https://wp-dd.com/how-to-rename-images-and-media-files-in-wordpress/

    Thanks.

    Thread Starter dragongate

    (@dragongate)

    Hello Sundar,

    Pls. note that image file name (for example image file name “x”) only change to x-1 or x-2 or x-3… depends on how many times you reload the same image file name only happen when you reload the same image file name in different date, for example:

    if you uploaded image “x” in one day then you deleted it and try to reload the same image x in different day (different date) the image file name change automatically to x-1.

    But if you uploaded image “x” in one day then you deleted it and try to reload the same image x in the same day (the same date) the image file name won’t change automatically to x-1.

    Please. try it and check yourself and you will notice that.

    Pls. help me if possible.
    Thanks.

    • This reply was modified 7 months, 3 weeks ago by dragongate.
    • This reply was modified 7 months, 3 weeks ago by dragongate.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Problem WP automatically change image filenames after upload’ is closed to new replies.