WordPress - How to check if user is logged in

Function to check if user is logged in is is_user_logged_in(). It would return true if user is logged in and false otherwise.

Function reference: is_user_logged_in

Example - iNove theme - footer.php file

By default Google Analytics code is displayed to everyone:

<?php
	wp_footer();

	$options = get_option('inove_options');
	if ($options['analytics']) {
		echo($options['analytics_content']);
	}
?>

After change the code is displayed only if user is not logged in:

<?php
	wp_footer();

	$options = get_option('inove_options');
	if ($options['analytics'] and !is_user_logged_in()) {
		echo($options['analytics_content']);
	}
?>
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a system administrator working for a successful Polish company and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu