du512053619
2016-01-22 05:03
浏览 36
已采纳

如何在上传时从WordPress后端和前端更改媒体文件的名称?

Suppose I uploaded a media file for eg image, and its name is example_123_56737_303030.png but before upload I want this name of the image to be changed as any thing of my choice (best will be post_id of this attachment) so it looks like 122.png (122 is the post_id of this attachment).

Now the things that needs to be achieved is :

  1. Name should be changed when I upload the file from back-end/front-end/directly in media/In post/In page/Any custom post type (means if media is uploaded from anywhere in the wordpress website).
  2. The serialize data which wordpress saves for every image, that are _wp_attached_file, _wp_attachment_metadata in wp_post_meta table should be as per the new name.
  3. The image should be of the new name in the folder as well (upload place/directory is fine no changes required in that).

图片转代码服务由CSDN问答提供 功能建议

假设我上传了例如image的媒体文件,其名称为 example_123_56737_303030.png 但在上传之前,我想要将图像的名称更改为我选择的任何内容(最好是此附件的post_id),所以它看起来像122.png(122是此附件的post_id)。 \ n

现在需要实现的是:

  1. 当我从后端/前端/上传文件时,应该更改名称 直接在媒体/在帖子/在页面/任何自定义帖子类型(意味着媒体是从wordpress网站的任何地方上传)。
  2. wordpress为每个图像保存的序列化数据,即< wp_post_meta 表中的代码> _wp_attached_file _wp_attachment_metadata 应该是新名称。
  3. 图像应该是新的 文件夹中的名称也是如此(上传 place /目录很好,不需要进行任何更改)。
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqiyang3711 2016-01-22 05:55
    已采纳

    Put below code into your functions.php

    function handle_uploadedimage($arr) {
    
            // Get the parent post ID, if there is one
            if( isset($_REQUEST['post_id']) ) {
                $post_id = $_REQUEST['post_id'];
            } else {
                $post_id = false;
            }
    
            // Only do this if we got the post ID--otherwise they're probably in
            //  the media section rather than uploading an image from a post.
    
            if($post_id && is_numeric($post_id)) {
    
                // Get the post slug
                $post_obj  = get_post($post_id); 
                $post_slug = $post_obj->post_name;
    
                // If we found a slug
                if($post_slug) {
                    $random_number = rand(10000,99999);
                    $ext = pathinfo($arr['name'], PATHINFO_EXTENSION);
                    $arr['name'] = $post_slug . '-' . $random_number . '.'.$ext;
                }
            }
            return $arr;
        }
        add_filter('wp_handle_upload_prefilter', 'handle_uploadedimage', 1, 1);
    

    You can also try below version

    function handle_uploadedimage($arr) {
    
            $random_number = md5(rand(10000,99999));
            $ext = pathinfo($arr['name'], PATHINFO_EXTENSION);
            $arr['name'] = $random_number .'.'.$ext;
    
            return $arr;
        }
        add_filter('wp_handle_upload_prefilter', 'handle_uploadedimage', 1, 1);
    

    You can also try this plugin wordpress.org/plugins/file-renaming-on-upload/screenshots which will be better I guess

    点赞 打赏 评论

相关推荐 更多相似问题