把下面的代码到您的的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);
您还可以试试下面的版本
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);
您也可以尝试这个插件wordpress.org/plugins/file-renaming-on-upload/screenshots这会更好我猜
不工作..文件名仍然是一样的 –
我修改了代码.. pl。检查 –
是啊!谢谢 –