Cách đổi tên ảnh upload tự động theo title trong WordPress

5/5 - (1 bình chọn)

Zubi Cloud hướng dẫn quý khách Cách đổi tên ảnh upload tự động theo title trong WordPress. Điều này rất quan trọng và ảnh hướng lớn đối với SEO images. Bằng việc lâu nay bạn làm thủ công đổi tên ảnh cho đúng, cho chuẩn, thì nay Zubi Cloud Support sẽ giúp quý vị tự động hóa quy trình đó, Đổi tên hàng loạt file ảnh theo thứ tự

Các bước đổi tên ảnh upload tự động theo title trong WordPress như sau:

Trước tiên, phải chèn Code tự động lưu ảnh vào hosting khi copy bài từ trang khác

Tiếp theo, Các bạn mở file function.php và chèn đoạn code sau:

function wp_modify_uploaded_file_names($image_name) {
// Get the parent post ID, if there is one
if( isset($_GET['post_id']) ) {
$post_id = $_GET['post_id'];
} elseif( isset($_POST['post_id']) ) {
$post_id = $_POST['post_id'];
}
// 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(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);
$image_name['name'] = $post_slug . '-' . $random_number . '.jpg';
}
}
return $image_name;
}
add_filter('wp_handle_upload_prefilter', 'wp_modify_uploaded_file_names', 1, 1);

Vậy là xong.

5/5 - (1 bình chọn)


Trả lời