force_ssl_admin()WP 2.6.0

Defines whether forced use of the SSL (https) protocol is enabled for the admin panel. Allows enabling/disabling SSL.

The function also allows enabling/disabling forced use of SSL for the admin part of WordPress. For example, if the function is called with the argument: force_ssl_admin(false), then this call is remembered and on the next call to it, it will return false.

The value of the constant FORCE_SSL_ADMIN is passed to the function at an early stage, which is set in wp-config.php. Based on this constant, the function determines whether to force the use of the HTTPS protocol in the admin area.

Used in other functions to check SSL in the admin area.

No Hooks.

Returns

true|false. True if forced use of SSL is set in the admin area. false otherwise.

Usage

force_ssl_admin( $force );
$force(string/bool)
Whether to force the use of SSL in the admin area.
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.8.1

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

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

	return $forced;
}