Ukázka kódu 2431

HTTP autentifikace pro web na WordPressu

add_action('init', function() {
	$user = 'user';
	$password = 'password';

	if ( is_admin() ) return;
	if ( is_user_logged_in() ) return;
	if ( isset($_GET['doing_wp_cron']) ) return;

	if ( isset( $_SERVER['PHP_AUTH_USER'] ) AND 
		$_SERVER['PHP_AUTH_USER'] == $user AND 
		$_SERVER['PHP_AUTH_PW'] == $password ) {
		return;
	}

	header('WWW-Authenticate: Basic realm="Protected area"');
	header('HTTP/1.0 401 Unauthorized');
	echo 'Protected area.';
	exit;

});