-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwp_all_import_image_filename.php
64 lines (57 loc) · 1.84 KB
/
wp_all_import_image_filename.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* =====================================
* Filter: wp_all_import_image_filename
* =====================================
*
* Can be used to set a custom filename for an imported image.
*
* Will be called for regular image imports.
*
* @since 4.3
*
* @param $filename string - The filename as created by the import
* @param $img_title string - From "SEO and advanced options".
* @param $img_caption string - From "SEO and advanced options".
* @param $img_alt string - From "SEO and advanced options".
* @param $article string
*
* @return string
*/
function wpai_image_filename($filename, $img_title, $img_caption, $img_alt, $article)
{
// Your code
return $filename;
}
add_filter('wp_all_import_image_filename', 'wpai_image_filename', 10, 5);
// ----------------------------
// Example uses below
// ----------------------------
/**
* Allows you to base the image name off the alt text. Useful when the image name
* generated is not unique for each image (uses "example.com/image?id=x" or similar format)
*
*/
function image_name_from_alt($filename, $img_title, $img_caption, $img_alt, $article)
{
if (!empty($img_alt)) {
$filename = sanitize_file_name($img_alt) . '.jpg';
}
return $filename;
}
add_filter('wp_all_import_image_filename', 'image_name_from_alt', 10, 5);
/**
* Allows you to base the image name off the URL hash. Useful when the image name
* generated is not unique for each image (uses "example.com/image?id=x" or similar format)
*
* For this to work, put the entire image URL in the img alt text field.
*
*/
function image_name_from_hash($filename, $img_title, $img_caption, $img_alt, $article)
{
if (!empty($img_alt)) {
$filename = substr(md5($img_alt), 0, 8) . ".jpg";
}
return $filename;
}
add_filter('wp_all_import_image_filename', 'image_name_from_hash', 10, 5);