Created
July 10, 2017 15:42
-
-
Save Kelderic/51a511afb041c5859e9f024d06065f4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FORCE ALL TRAFFIC TO USE SSL | |
add_filter( 'admin_init' , 'prefix_add_force_ssl_setting' ); | |
add_action('template_redirect', 'prefix_force_ssl'); | |
function prefix_add_force_ssl_setting() { | |
register_setting( 'general', 'prefix_force_ssl', 'esc_attr' ); | |
add_settings_field('prefix_force_ssl', 'Force SSL' , function() { | |
echo ' | |
<fieldset> | |
<legend class="screen-reader-text"><span>Force SSL</span></legend> | |
<label for="prefix_force_ssl"> | |
<input type="checkbox" name="prefix_force_ssl" id="prefix_force_ssl" value="1" ' . checked('1', get_option('prefix_force_ssl'), false) . ' /> | |
Redirect all users to SSL versions of pages. | |
</label> | |
<p><i>(Only check this if you have a valid SSL certificate installed!)</i></p> | |
</fieldset> | |
'; | |
} , 'general' ); | |
} | |
function prefix_force_ssl() { | |
if ( ! is_ssl() && get_option('prefix_force_ssl') == '1' ) { | |
wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 ); | |
exit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this to a theme's
functions.php
file, to give your theme an option in the Settings->General page. If checked, all traffic will be redirected to anhttps
version of the requested page.Obviously this requires that your server will serve over
https
, which requires you have a valid SSL certificate.