Here’s an example using the is_user_logged_in() function to display a logout link for logged-in users and a login link for logged-out users.
<?php
if ( is_user_logged_in() ) {
echo 'Welcome, logged-in user. <a href="'.wp_logout_url().'">Click here to logout</a>.';
}else{
echo 'Please login by <a href="'.wp_login_url().'">clicking here</a>.'
}
We can use this in our theme’s function.php to add functionality specific to logged-in users. It will also work in our theme’s index.php, archive.php, single.php, etc for all kinds of functionality for logged-in users.