How to Check user is logged in or not

In wordpress we might want to check user is logged in or not.
This can be easily done by using some of the inbuilt worpress functions.Here we will use is_user_logged_in() function.
Here is sample code to check user is logged in or not.

<?php
if (is_user_logged_in()) { ?>
<?php } ?>
<?php
if ( is_user_logged_in() ) { ?>
<a href="<?php echo wp_logout_url(); ?>" title="Logout">Logout</a>
<?php } else { ?>
<a href="<?php echo wp_login_url(); ?>" title="Login">Login</a>
<?php } ?>

If user is logged in then Logout link is shown using

<a href="<?php echo wp_logout_url(); ?>" title="Logout">Logout</a>

On click of logout it will redirect to wp-admin login page.
Redirection after clicking logout can be changed also.If we want to redirect to current page after logout then use bellow .

<a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout">Logout</a>

If we want to redirect to Home Page page after logout then use bellow

<a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Logout</a>

For More information about Logout Link Click Here

If user is not logged in then Login link is shown using bellow link.Link will redirect to wordpress login form from where user can login.

<a href="<?php echo wp_login_url(); ?>" title="Login">Login</a>

After user have logged in, user will be redirected to wordpress dashboard.This redirection can be also changed using some other links insted of default login link.

If you want user to redirect to same page then use bellow link.

<a href="<?php echo wp_login_url( get_permalink() ); ?>" title="Login">Login</a>

If you want user to redirect to homepage then use bellow link.

<a href="<?php echo wp_login_url( home_url() ); ?>" title="Login">Login</a>

For more information about Login link Click Here

This is how we can check whether user is logged in or not in wordpress.

admin