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.

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!

share|improve this question

migrated from stackoverflow.com Jul 4 '12 at 2:51

This question came from our site for professional and enthusiast programmers.

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

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