0

Is there any way to change the image name during uploading in Wordpress, but with one requirement - the new file names should be going sequentially, not in hash?

There was a very similar question with a fine answer here, but the new characters are going in hash there, not in order.

Thank you for any help!

1 Answer 1

3

The sequencing part will be handled automatically by wordpress.

if you want to change the filename, you can do this :

function k99_custom_upload_name($filename)
{
  $info = pathinfo($filename);
  $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
  $filename  =  'put your name here';
     return $filename;
 }

add_filter('sanitize_file_name', 'k99_custom_upload_name', 10);

Because wordpress will handle the sequencing , your files will be named :

  • put your name here.ext
  • put your name here1.ext
  • put your name here2.ext

etc.. etc..

an alternative (shorter) way , that would not require to handle the extension would be :

function k99_custom_upload_name($filename)
{
$post_for_id = $post->post_parent;
    $filename= 'put your name here -'.$filename;
    return $filename;
}

add_filter('sanitize_file_name', 'k99_custom_upload_name', 10);

In this case your files will be renamed like so :

  • put your name here-originalname.ext
  • put your name here-originalname1.ext
  • put your name here-originalname2.ext

etc.. etc..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.