Skip to content

Instantly share code, notes, and snippets.

@fjarrett
Last active September 19, 2015 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjarrett/8cd04bd33e49bbd6e3b7 to your computer and use it in GitHub Desktop.
Save fjarrett/8cd04bd33e49bbd6e3b7 to your computer and use it in GitHub Desktop.
Force WP JSON REST API endpoints to always be served over HTTPS
<?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' );
@fjarrett
Copy link
Author

This is also available as a plugin https://github.com/fjarrett/json-rest-api-force-ssl

@vancoder
Copy link

is_ssl() should be ! is_ssl()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment