Last active
September 19, 2015 09:04
-
-
Save frankiejarrett/8cd04bd33e49bbd6e3b7 to your computer and use it in GitHub Desktop.
Force WP JSON REST API endpoints to always be served over HTTPS
This file contains 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
<?php | |
/** | |
* Force WP JSON REST API endpoints to always be served over HTTPS | |
* | |
* @action wp_json_server_before_serve | |
* | |
* @return void | |
*/ | |
function fjarrett_wp_json_force_ssl() { | |
if ( is_ssl() ) { | |
return; | |
} | |
$json_url = esc_url_raw( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); | |
$redirect = set_url_scheme( $json_url, 'https' ); | |
wp_safe_redirect( $redirect, 301 ); | |
exit; | |
} | |
add_action( 'wp_json_server_before_serve', 'fjarrett_wp_json_force_ssl' ); |
is_ssl() should be ! is_ssl()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is also available as a plugin https://github.com/fjarrett/json-rest-api-force-ssl