当前位置:网站首页>实现WordPress上传图片自动重命名的方法
实现WordPress上传图片自动重命名的方法
2022-06-27 12:54:00 【你的明明呐丶】
在我们使用 WordPress 发布文章时,经常都需要添加图片、多媒体什么的。
然而,大家都知道 WordPress 是舶来物,对于中文用户来说,我们都会把图片命名为中文的,由于 WordPress 机制的原因,并不能正常的显示图片或者各种的问题。
那么,如何让 WordPress 图片自动重命名然后正常显示使用呢?
现在,就和大家说说更加接地气一点的 WordPress 技巧,就是在我们上传图片的时候,能够让图片的名称自动变成英文或数字。
具体有根据上传时间命名和MD5转码文件名为32位字符串两种格式的文件命名方法,只能二选一。
一、根据上传的时间命名
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;
}
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
二、文件名md5转码为32位字符串
function custom_upload_filter( $file ){
$info = pathinfo($file['name']);
$ext = '.' . $info['extension'];
$md5 = md5($file['name']);
$file['name'] = $md5.$ext;
return $file;
}
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
食用方法
将代码添加到所使用主题的 functions.php 文件中即可。
以上两种方法实现WordPress上传图片自动重命名的方法只能二选一,不可同时使用两种方法,要不然出错的话后果自负哦。
边栏推荐
- 7 killer JS lines of code
- 基于JSP实现医院病历管理系统
- [tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)
- What kind of air conditioner is this?
- Details of istio micro service governance grid traffic management core resource controller
- ViewPager2使用记录
- Quick news: Huawei launched the Hongmeng developer competition; Tencent conference released the "Wanshi Ruyi" plan
- Intranet learning notes (8)
- 今日睡眠质量记录78分
- How to download pictures with hyperlinks
猜你喜欢
随机推荐
浅谈软件研发的复杂性与效能提升之道
【周赛复盘】LeetCode第81场双周赛
Esp32s3 iperf routine test esp32s3 throughput test
Make learning pointer easier (1)
诗歌一首看看
手把手教你搭一个永久运行的个人服务器!
AI for Science: scientific research paradigm, open source platform and industrial form
gcc编译动态库和静态库
推荐系统的下一步?阿里时空聚合GNN,效果吊打LightGCN!
万物互联时代到来,锐捷发布场景化无线零漫游方案
Privacy computing fat offline prediction
What else can PLM do?
Review summary of database
Convn-n dimensional convolution
It is so simple to remove the payment restrictions on VIP, YuQue and Zhihu in Baidu Library
Size end byte order
How to open an account for CSI 500 stock index futures, what are the regular domestic stock index futures platforms, and where is the safest place to open an account?
Viewpager2 usage record
#yyds干货盘点# 解决剑指offer:剪绳子(进阶版)
深信服X计划-系统基础总结