Thêm ký tự ngẩu nhiên vào tên file upload trong WordPress

Wordpress Giza Data Tháng hai 14, 2023

Code này có chức năng thêm 7 ký tự ngẩu nhiên trước tên file khi upload trong WordPress.

Chèn code vào file funtion của theme.

function prefix_random_string_to_filename($filename) {
  $info = pathinfo($filename);
  $ext = isset($info['extension']) ? $info['extension'] : '';
  $name = isset($info['filename']) ? $info['filename'] : '';
  $newname = substr(md5(rand()), 0, 7) . '-' . $name . '.' . $ext;
  return $newname;
}
add_filter('sanitize_file_name', 'prefix_random_string_to_filename', 10);

Hãy tuỳ biến theo nhu cầu, chúc bạn thành công.

Quảng Cáo