I am working on a plugin. In plugin, there is a custom media uploader. All images upload successfully.

Then I want that when I upload images its renamed, so i get this code and write it in my plugin file.

function foo_rename_image($filename) {
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    return "foo_".$name."_".date('dmy-his').$ext;
add_filter('sanitize_file_name', 'foo_rename_image', 10);

It renamed images successfully when its uploaded. But there is one issue, its renamed all images.

e.g. My plugin creates a post type products and there is a custom media uploader so I upload an image its renamed, then I go to 'add post' or 'add page' and there I upload an image and its also renamed. I just want to rename images that uploaded by my plugin or custom post type.

So please give me an idea that when I upload images from my plugin or my custom post type then its renamed. Otherwise its upload default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.