3
3
Plugin Name: Force HTTPS
4
4
Plugin URI: https://www.littlebizzy.com/plugins/force-https
5
5
Description: HTTPS enforcement for WordPress
6
- Version: 2.0.4
6
+ Version: 2.1.0
7
7
Requires PHP: 7.0
8
8
Author: LittleBizzy
9
9
Author URI: https://www.littlebizzy.com
25
25
}, 999 );
26
26
27
27
// force https redirection for all non-https requests
28
- add_action ( 'init ' , function () {
28
+ add_action ( 'init ' , 'force_https_redirect_non_https ' , 10 );
29
+ function force_https_redirect_non_https () {
29
30
if ( ! is_ssl () && ! is_admin () && PHP_SAPI !== 'cli ' ) {
30
31
if ( ! headers_sent () ) {
31
32
$ redirect_url = home_url ( add_query_arg ( null , null ) );
32
33
wp_safe_redirect ( set_url_scheme ( $ redirect_url , 'https ' ), 301 );
33
34
exit ;
34
35
}
35
36
}
36
- }, 10 );
37
+ }
37
38
38
39
// force https on all urls by replacing http:// with https://
39
- function fhttps_securize_url ( $ url ) {
40
+ function force_https_securize_url ( $ url ) {
40
41
if ( strpos ( $ url , 'http:// ' ) === 0 ) {
41
42
return set_url_scheme ( $ url , 'https ' );
42
43
}
43
44
return $ url ;
44
45
}
45
46
46
47
// apply https to all relevant wordpress filters
47
- add_filter ( 'script_loader_src ' , 'fhttps_securize_url ' , 20 );
48
- add_filter ( 'style_loader_src ' , 'fhttps_securize_url ' , 20 );
49
- add_filter ( 'wp_get_attachment_url ' , 'fhttps_securize_url ' , 20 );
50
- add_filter ( 'the_permalink ' , 'fhttps_securize_url ' , 20 );
51
- add_filter ( 'post_link ' , 'fhttps_securize_url ' , 20 );
52
- add_filter ( 'page_link ' , 'fhttps_securize_url ' , 20 );
53
- add_filter ( 'term_link ' , 'fhttps_securize_url ' , 20 );
54
- add_filter ( 'home_url ' , 'fhttps_securize_url ' , 20 );
55
- add_filter ( 'site_url ' , 'fhttps_securize_url ' , 20 );
56
- add_filter ( 'network_site_url ' , 'fhttps_securize_url ' , 20 );
57
- add_filter ( 'network_home_url ' , 'fhttps_securize_url ' , 20 );
58
- add_filter ( 'template_directory_uri ' , 'fhttps_securize_url ' , 20 );
59
- add_filter ( 'stylesheet_directory_uri ' , 'fhttps_securize_url ' , 20 );
60
- add_filter ( 'get_avatar_url ' , 'fhttps_securize_url ' , 20 );
61
- add_filter ( 'rest_url ' , 'fhttps_securize_url ' , 20 );
48
+ add_filter ( 'script_loader_src ' , 'force_https_securize_url ' , 20 );
49
+ add_filter ( 'style_loader_src ' , 'force_https_securize_url ' , 20 );
50
+ add_filter ( 'wp_get_attachment_url ' , 'force_https_securize_url ' , 20 );
51
+ add_filter ( 'the_permalink ' , 'force_https_securize_url ' , 20 );
52
+ add_filter ( 'post_link ' , 'force_https_securize_url ' , 20 );
53
+ add_filter ( 'page_link ' , 'force_https_securize_url ' , 20 );
54
+ add_filter ( 'term_link ' , 'force_https_securize_url ' , 20 );
55
+ add_filter ( 'home_url ' , 'force_https_securize_url ' , 20 );
56
+ add_filter ( 'site_url ' , 'force_https_securize_url ' , 20 );
57
+ add_filter ( 'network_site_url ' , 'force_https_securize_url ' , 20 );
58
+ add_filter ( 'network_home_url ' , 'force_https_securize_url ' , 20 );
59
+ add_filter ( 'template_directory_uri ' , 'force_https_securize_url ' , 20 );
60
+ add_filter ( 'stylesheet_directory_uri ' , 'force_https_securize_url ' , 20 );
61
+ add_filter ( 'get_avatar_url ' , 'force_https_securize_url ' , 20 );
62
+ add_filter ( 'rest_url ' , 'force_https_securize_url ' , 20 );
62
63
63
64
// ensure all urls in the upload directory use https
64
- add_filter ( 'upload_dir ' , function ( $ uploads ) {
65
+ add_filter ( 'upload_dir ' , 'force_https_fix_upload_dir ' , 20 );
66
+ function force_https_fix_upload_dir ( $ uploads ) {
65
67
$ uploads ['url ' ] = set_url_scheme ( $ uploads ['url ' ], 'https ' );
66
68
$ uploads ['baseurl ' ] = set_url_scheme ( $ uploads ['baseurl ' ], 'https ' );
67
69
return $ uploads ;
68
- } );
70
+ }
69
71
70
72
// apply https to all elements and attributes that can contain urls
71
- add_filter ( 'the_content ' , function ( $ content ) {
72
- $ content = fhttps_convert_urls_to_https ( $ content );
73
- $ content = fhttps_convert_inline_styles_to_https ( $ content );
73
+ add_filter ( 'the_content ' , 'force_https_process_content ' , 20 );
74
+ function force_https_process_content ( $ content ) {
75
+ $ content = force_https_convert_urls_to_https ( $ content );
76
+ $ content = force_https_convert_inline_styles_to_https ( $ content );
74
77
return $ content ;
75
- }, 20 );
78
+ }
76
79
77
80
// helper function: convert all resource and hyperlink urls to https
78
- function fhttps_convert_urls_to_https ( $ content ) {
81
+ function force_https_convert_urls_to_https ( $ content ) {
79
82
return preg_replace_callback (
80
83
'#(<(?:img|iframe|embed|source|script|link|meta|video|audio|track|object|form|area|input|button|a)[^>]+(?:src|srcset|data-src|data-href|action|poster|content|style|href|manifest)=[" \'])(http://)([^" \']+)# ' ,
81
84
function ( $ matches ) {
@@ -86,7 +89,7 @@ function( $matches ) {
86
89
}
87
90
88
91
// helper function: convert inline styles with url() to https
89
- function fhttps_convert_inline_styles_to_https ( $ content ) {
92
+ function force_https_convert_inline_styles_to_https ( $ content ) {
90
93
return preg_replace_callback (
91
94
'#(<[^>]+(?:style)=[" \'][^>]*?url\((http://)([^" \']+)\))# ' ,
92
95
function ( $ matches ) {
@@ -97,35 +100,41 @@ function( $matches ) {
97
100
}
98
101
99
102
// enforce https for text widget content
100
- add_filter ( 'widget_text ' , function ( $ content ) {
103
+ add_filter ( 'widget_text ' , 'force_https_fix_widget_text ' , 20 );
104
+ function force_https_fix_widget_text ( $ content ) {
101
105
return str_replace ( 'http:// ' , 'https:// ' , $ content );
102
- }, 20 );
106
+ }
103
107
104
108
// enforce https for widget text content in newer wordpress versions
105
- add_filter ( 'widget_text_content ' , function ( $ content ) {
109
+ add_filter ( 'widget_text_content ' , 'force_https_fix_widget_text_content ' , 20 );
110
+ function force_https_fix_widget_text_content ( $ content ) {
106
111
return str_replace ( 'http:// ' , 'https:// ' , $ content );
107
- }, 20 );
112
+ }
108
113
109
114
// apply https to all urls in custom menus
110
- add_filter ( 'nav_menu_link_attributes ' , function ( $ atts ) {
115
+ add_filter ( 'nav_menu_link_attributes ' , 'force_https_fix_menu_links ' , 20 );
116
+ function force_https_fix_menu_links ( $ atts ) {
111
117
if ( isset ( $ atts ['href ' ] ) && strpos ( $ atts ['href ' ], 'http:// ' ) === 0 ) {
112
118
$ atts ['href ' ] = set_url_scheme ( $ atts ['href ' ], 'https ' );
113
119
}
114
120
return $ atts ;
115
- }, 20 );
121
+ }
116
122
117
123
// enforce https for oembed urls
118
- add_filter ( 'embed_oembed_html ' , function ( $ html ) {
124
+ add_filter ( 'embed_oembed_html ' , 'force_https_fix_oembed_html ' , 20 );
125
+ function force_https_fix_oembed_html ( $ html ) {
119
126
return str_replace ( 'http:// ' , 'https:// ' , $ html );
120
- }, 20 );
127
+ }
121
128
122
129
// enforce https for any urls used in shortcodes
123
- add_filter ( 'do_shortcode_tag ' , function ( $ output ) {
130
+ add_filter ( 'do_shortcode_tag ' , 'force_https_fix_shortcode_urls ' , 20 );
131
+ function force_https_fix_shortcode_urls ( $ output ) {
124
132
return str_replace ( 'http:// ' , 'https:// ' , $ output );
125
- }, 20 );
133
+ }
126
134
127
135
// enforce https on wp_resource_hints
128
- add_filter ( 'wp_resource_hints ' , function ( $ urls ) {
136
+ add_filter ( 'wp_resource_hints ' , 'force_https_fix_resource_hints ' , 20 );
137
+ function force_https_fix_resource_hints ( $ urls ) {
129
138
if ( is_array ( $ urls ) ) {
130
139
foreach ( $ urls as &$ url ) {
131
140
if ( is_array ( $ url ) && isset ( $ url ['href ' ] ) ) {
@@ -136,10 +145,11 @@ function( $matches ) {
136
145
}
137
146
}
138
147
return $ urls ;
139
- }, 20 );
148
+ }
140
149
141
150
// enforce https on attachment metadata
142
- add_filter ( 'wp_get_attachment_metadata ' , function ( $ data ) {
151
+ add_filter ( 'wp_get_attachment_metadata ' , 'force_https_fix_attachment_metadata ' , 20 );
152
+ function force_https_fix_attachment_metadata ( $ data ) {
143
153
if ( isset ( $ data ['file ' ] ) ) {
144
154
$ data ['file ' ] = str_replace ( 'http:// ' , 'https:// ' , $ data ['file ' ] );
145
155
}
@@ -151,28 +161,30 @@ function( $matches ) {
151
161
}
152
162
}
153
163
return $ data ;
154
- }, 20 );
164
+ }
155
165
156
166
// enforce https on image srcsets
157
- add_filter ( 'wp_calculate_image_srcset ' , function ( $ sources ) {
167
+ add_filter ( 'wp_calculate_image_srcset ' , 'force_https_fix_image_srcsets ' , 20 );
168
+ function force_https_fix_image_srcsets ( $ sources ) {
158
169
foreach ( $ sources as &$ source ) {
159
170
if ( isset ( $ source ['url ' ] ) ) {
160
171
$ source ['url ' ] = str_replace ( 'http:// ' , 'https:// ' , $ source ['url ' ] );
161
172
}
162
173
}
163
174
return $ sources ;
164
- }, 20 );
175
+ }
165
176
166
177
// enforce https on custom logo html
167
- add_filter ( 'get_custom_logo ' , function ( $ html ) {
178
+ add_filter ( 'get_custom_logo ' , 'force_https_fix_custom_logo ' , 20 );
179
+ function force_https_fix_custom_logo ( $ html ) {
168
180
return str_replace ( 'http:// ' , 'https:// ' , $ html );
169
- }, 20 );
181
+ }
170
182
171
183
// enforce https for login/logout redirect urls
172
- add_filter ( 'login_redirect ' , 'fhttps_securize_url ' , 20 );
173
- add_filter ( 'logout_redirect ' , 'fhttps_securize_url ' , 20 );
184
+ add_filter ( 'login_redirect ' , 'force_https_securize_url ' , 20 );
185
+ add_filter ( 'logout_redirect ' , 'force_https_securize_url ' , 20 );
174
186
175
187
// ensure redirects are https
176
- add_filter ( 'wp_redirect ' , 'fhttps_securize_url ' , 20 );
188
+ add_filter ( 'wp_redirect ' , 'force_https_securize_url ' , 20 );
177
189
178
190
// Ref: ChatGPT
0 commit comments