使用WordPress做站的朋友们几乎都有频繁搬迁网站的习惯,那么你一定遇到过搬家后图片名称乱码无法显示的问题,今天就分享给大家 WordPress上传图片重命名为年份月份日期时间和随机数字的办法:
WordPress上传多媒体的代码都存放于\wp-admin\includes\里面的file.php,打开这个文件,
filename=wpuniquefilename(uploads[‘path’], file[′name′],unique_filename_callback );// Move the file to the uploads dir,在此句$filename赋值前添加代码如下:
$time=date("YmdHis");$type_change = array('image'=>'.');$file_type = strtr($file['type'],$type_change); $file['name'] = $time."".mt_rand(1,100)."".$file_type ;
添加后的完整代码如下:
$time=date("YmdHis");$type_change = array('image'=>'.');$file_type = strtr($file['type'],$type_change); $file['name'] = $time.''.mt_rand(1,100).''.$file_type ; $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback ); // Move the file to the uploads dir
覆盖file.php文件后,上传多媒体后文件就自动重命名为2015072615450088这样的格式了,年份月份日期时间和随机数字的形式也比较方便区分和维护。
" />