Rename files during upload within Wordpress backend

前端 未结 6 1719
清歌不尽
清歌不尽 2020-12-08 16:57

is there a way to rename files during the upload progress within the Wordpress 3.0 backend? I would like to have a consistent naming of files, especially for im

6条回答
  •  醉酒成梦
    2020-12-08 17:07

    http://wpapi.com/change-image-name-to-wordpress-post-slug-during-upload/

    BTW:

    Add filter to sanitize_file_name is totally wrong, as sanitize_file_name() function is a helper function to format string, it may be used elsewhere like plugins or themes.

    function wp_modify_uploaded_file_names($file) {
        $info = pathinfo($file['name']);
        $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
        $name = basename($file['name'], $ext);
    
        $file['name'] = uniqid() . $ext; // uniqid method
        // $file['name'] = md5($name) . $ext; // md5 method
        // $file['name'] = base64_encode($name) . $ext; // base64 method
    
        return $file;
    }
    
    add_filter('wp_handle_upload_prefilter', 'wp_modify_uploaded_file_names', 1, 1);
    

提交回复
热议问题