-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathimages.php
More file actions
150 lines (117 loc) · 2.99 KB
/
images.php
File metadata and controls
150 lines (117 loc) · 2.99 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Disable jpg image compression
*
* @link https://developer.wordpress.org/reference/hooks/jpeg_quality/
*/
add_filter(
'jpeg_quality',
function () {
return 100;
},
10,
2
);
/**
* Customize the minimum image pixels size for when fetchpriority="high" is set
*
* @link https://developer.wordpress.org/reference/hooks/wp_min_priority_img_pixels/
*/
add_filter(
'wp_min_priority_img_pixels',
function () {
return 2000 * 2000;
}
);
/**
* Customize the image sizes attribute string
*
* @link https://developer.wordpress.org/reference/hooks/wp_calculate_image_sizes/
*/
add_filter(
'wp_calculate_image_sizes',
function ( $size ) {
return 'auto, ' . $size;
},
10,
1
);
/**
* Remove image sizes (and create new ones)
*
* @link https://developer.wordpress.org/reference/functions/remove_image_size/
*/
add_action(
'after_setup_theme',
function () {
remove_image_size( '1536x1536' );
remove_image_size( '2048x2048' );
}
);
/**
* Set which image sizes are used in srcset
*
* @link https://developer.wordpress.org/reference/hooks/wp_calculate_image_srcset/
*/
add_filter(
'wp_calculate_image_srcset',
function ( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
unset( $sources[1024] );
unset( $sources[1536] );
return $sources;
},
10,
5
);
/**
* Remove srcset on images
*
* @link https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/
*/
add_filter( 'wp_calculate_image_srcset', '__return_false' );
/**
* Set default image attachment options
*
* @link https://developer.wordpress.org/apis/handbook/options/
*/
add_action(
'after_setup_theme',
function () {
// Remove default link
if ( get_option( 'image_default_link_type' ) !== 'none' ) {
update_option( 'image_default_link_type', 'none' );
}
// Remove default alignment
if ( get_option( 'image_default_align' ) !== 'none' ) {
update_option( 'image_default_align', 'none' );
}
// Set default size
if ( get_option( 'image_default_size' ) !== 'large' ) {
update_option( 'image_default_size', 'large' );
}
}
);
/**
* Remove lazy loading
*
* @link https://developer.wordpress.org/reference/hooks/wp_lazy_loading_enabled/
*/
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
add_filter( 'wp_lazy_loading_enabled', '__return_false', 'img' ); // disable only on img elements
add_filter( 'wp_lazy_loading_enabled', '__return_false', 'iframe' ); // disable only on iframe elements
/**
* Remove size attributes on images
*
* @param String $html
*/
if ( ! function_exists( 'remove_size_attributes' ) ) {
function remove_size_attributes( $html ) {
return preg_replace( '/(width|height)="\d*"/', '', $html );
}
// Remove size attributes from thumbnail images
add_filter( 'post_thumbnail_html', 'remove_size_attributes' );
// Remove size attributes in the editor
add_filter( 'image_send_to_editor', 'remove_size_attributes' );
// Remove size attributes from the_content
add_filter( 'the_content', 'remove_size_attributes' );
}