force_ssl_admin()WP 2.6.0

Whether to force SSL used for the Administration Screens.

No Hooks.

Return

true|false. True if forced, false if not forced.

Usage

force_ssl_admin( $force );
$force(string|true|false)
Whether to force SSL in admin screens.
Default: null

Examples

0

#1 Changing the return value

force_ssl_admin( true );
if ( force_ssl_admin() )
	echo 'The admin must use SSL';
else
	echo 'This code will never run';

force_ssl_admin( false );
if ( force_ssl_admin() )
	echo 'This code will never run';
else
	echo 'The admin area should NOT use SSL';
0

#2 Forced redirection to https

Redirect current page to https if current protocol is http:

if ( force_ssl_admin() && ! is_ssl() ) {

	if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
		wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
		exit;
	}
	else {
		wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
		exit;
	}
}
0

#3 Reset to default settings

force_ssl_admin( FORCE_SSL_ADMIN );

Changelog

Since 2.6.0 Introduced.

force_ssl_admin() code WP 6.3

function force_ssl_admin( $force = null ) {
	static $forced = false;

	if ( ! is_null( $force ) ) {
		$old_forced = $forced;
		$forced     = $force;
		return $old_forced;
	}

	return $forced;
}