Use the is_user_logged_in() function

The is_user_logged_in() function is a built-in WordPress function that determines whether the current visitor is a logged-in user. The function works by checking the current user’s data using the wp_get_current_user() function and then verifying if the user exists.

Here is the PHP code snippet for the is_user_logged_in() function:


function is_user_logged_in() {
	$user = wp_get_current_user();

	return $user->exists();
}

Use Cases

WordPress developers can use the is_user_logged_in() function in various situations, including:

 

1.Conditional content display

By using the is_user_logged_in() function, developers can create conditional statements that display specific content to logged-in users only. For example, they can hide certain widgets, menus, or sections of a page from non-logged-in users.


if ( is_user_logged_in() ) {
    // Display content for logged-in users
    echo 'Welcome, ' . wp_get_current_user()->display_name;
} else {
    // Display content for non-logged-in users
    echo 'Please log in to access this content.';
}

 

2.Custom redirections:

Developers can also use the is_user_logged_in() function to redirect users to different pages or URLs based on their authentication status. For example, they can redirect non-logged-in users to the login page, while redirecting logged-in users to their account dashboard.


function custom_redirect() {
    if ( is_user_logged_in() ) {
        // Redirect logged-in users to their account dashboard
        wp_redirect( site_url( '/account-dashboard/' ) );
    } else {
        // Redirect non-logged-in users to the login page
        wp_redirect( wp_login_url() );
    }
    exit;
}
add_action( 'template_redirect', 'custom_redirect' );

 

3.Restricting access to certain pages or content:

Developers can use the is_user_logged_in() function in combination with other WordPress functions such as current_user_can() to restrict access to specific pages or content on their site. For example, they can restrict access to certain pages or posts to logged-in users only.


function restrict_access_to_page() {
    // The ID of the page you want to restrict access to
    $restricted_page_id = 123;

    if ( is_page( $restricted_page_id ) && ! is_user_logged_in() ) {
        // Redirect non-logged-in users to the login page if they try to access the restricted page
        wp_redirect( wp_login_url( get_permalink( $restricted_page_id ) ) );
        exit;
    }
}
add_action( 'template_redirect', 'restrict_access_to_page' );

In summary, the is_user_logged_in() function is a versatile function that can be used by WordPress developers to customize their site’s behavior based on the current user’s authentication status.

 

How to Apply This Function

To use the is_user_logged_in() function, you’ll need to be familiar with programming in PHP. If you are editing a theme, best practice is to create a child theme first. This will allow you to edit code in your theme without it breaking when the parent theme is updated.

However, I recommend using the WPCode plugin, which offers an easy and flexible way to organize and implement custom code in your WordPress site. The WPCode plugin enables developers to insert custom PHP code snippets without directly editing their theme files, thus ensuring better organization and easier management.