Skip to content

Commit bf859f6

Browse files
authored
Update force-https.php
1 parent c5c23af commit bf859f6

File tree

1 file changed

+60
-48
lines changed

1 file changed

+60
-48
lines changed

force-https.php

+60-48
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Force HTTPS
44
Plugin URI: https://www.littlebizzy.com/plugins/force-https
55
Description: HTTPS enforcement for WordPress
6-
Version: 2.0.4
6+
Version: 2.1.0
77
Requires PHP: 7.0
88
Author: LittleBizzy
99
Author URI: https://www.littlebizzy.com
@@ -25,57 +25,60 @@
2525
}, 999 );
2626

2727
// 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() {
2930
if ( ! is_ssl() && ! is_admin() && PHP_SAPI !== 'cli' ) {
3031
if ( ! headers_sent() ) {
3132
$redirect_url = home_url( add_query_arg( null, null ) );
3233
wp_safe_redirect( set_url_scheme( $redirect_url, 'https' ), 301 );
3334
exit;
3435
}
3536
}
36-
}, 10 );
37+
}
3738

3839
// force https on all urls by replacing http:// with https://
39-
function fhttps_securize_url( $url ) {
40+
function force_https_securize_url( $url ) {
4041
if ( strpos( $url, 'http://' ) === 0 ) {
4142
return set_url_scheme( $url, 'https' );
4243
}
4344
return $url;
4445
}
4546

4647
// 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 );
6263

6364
// 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 ) {
6567
$uploads['url'] = set_url_scheme( $uploads['url'], 'https' );
6668
$uploads['baseurl'] = set_url_scheme( $uploads['baseurl'], 'https' );
6769
return $uploads;
68-
} );
70+
}
6971

7072
// 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 );
7477
return $content;
75-
}, 20 );
78+
}
7679

7780
// 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 ) {
7982
return preg_replace_callback(
8083
'#(<(?: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://)([^"\']+)#',
8184
function( $matches ) {
@@ -86,7 +89,7 @@ function( $matches ) {
8689
}
8790

8891
// 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 ) {
9093
return preg_replace_callback(
9194
'#(<[^>]+(?:style)=["\'][^>]*?url\((http://)([^"\']+)\))#',
9295
function( $matches ) {
@@ -97,35 +100,41 @@ function( $matches ) {
97100
}
98101

99102
// 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 ) {
101105
return str_replace( 'http://', 'https://', $content );
102-
}, 20 );
106+
}
103107

104108
// 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 ) {
106111
return str_replace( 'http://', 'https://', $content );
107-
}, 20 );
112+
}
108113

109114
// 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 ) {
111117
if ( isset( $atts['href'] ) && strpos( $atts['href'], 'http://' ) === 0 ) {
112118
$atts['href'] = set_url_scheme( $atts['href'], 'https' );
113119
}
114120
return $atts;
115-
}, 20 );
121+
}
116122

117123
// 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 ) {
119126
return str_replace( 'http://', 'https://', $html );
120-
}, 20 );
127+
}
121128

122129
// 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 ) {
124132
return str_replace( 'http://', 'https://', $output );
125-
}, 20 );
133+
}
126134

127135
// 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 ) {
129138
if ( is_array( $urls ) ) {
130139
foreach ( $urls as &$url ) {
131140
if ( is_array( $url ) && isset( $url['href'] ) ) {
@@ -136,10 +145,11 @@ function( $matches ) {
136145
}
137146
}
138147
return $urls;
139-
}, 20 );
148+
}
140149

141150
// 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 ) {
143153
if ( isset( $data['file'] ) ) {
144154
$data['file'] = str_replace( 'http://', 'https://', $data['file'] );
145155
}
@@ -151,28 +161,30 @@ function( $matches ) {
151161
}
152162
}
153163
return $data;
154-
}, 20 );
164+
}
155165

156166
// 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 ) {
158169
foreach ( $sources as &$source ) {
159170
if ( isset( $source['url'] ) ) {
160171
$source['url'] = str_replace( 'http://', 'https://', $source['url'] );
161172
}
162173
}
163174
return $sources;
164-
}, 20 );
175+
}
165176

166177
// 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 ) {
168180
return str_replace( 'http://', 'https://', $html );
169-
}, 20 );
181+
}
170182

171183
// 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 );
174186

175187
// ensure redirects are https
176-
add_filter( 'wp_redirect', 'fhttps_securize_url', 20 );
188+
add_filter( 'wp_redirect', 'force_https_securize_url', 20 );
177189

178190
// Ref: ChatGPT

0 commit comments

Comments
 (0)