用WordPress写文章时,经常需要上传图片、多媒体。WordPress默认是不会给图片命名,就会导致图片URL会出现中文或重名,那么,如何让WordPress图片重命名呢?本文将分享WordPress上传图片自动重命名方法,让图片上传以后,文件能在我们上传的时候自动变成英文或数字,更美观简洁。
选择以下任意一段代码放置在主题的functions.php文件中即可:
方法一:
//根据上传时间重命名文件 add_filter('wp_handle_upload_prefilter', 'custom_upload_filter'); function custom_upload_filter($file){ $info = pathinfo($file['name']); $ext = $info['extension']; $filedate = date('YmdHis').rand(10,99);//为了避免时间重复,再加一段2位的随机数 $file['name'] = $filedate.'.'.$ext; return $file; }
方法二:
//使用md5转码文件名 add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' ); function custom_upload_filter($file){ $info = pathinfo($file['name']); $ext = '.' . $info['extension']; $md5 = md5($file['name']); $file['name'] = $md5.$ext; return $file; }
原文链接:https://www.aiwre.com/knowledge/04331.html,转载请注明出处~~~
评论0