WordPress Logic: If Is Logged In
There are several tidbits of code that I have collected over the past few years that make it easier and easier to turn a simple WordPress installation into a very functional content management system (CMS). There are many times when it would be nice to show logged in members certain bits of information (certain categories, posts, or just a simple “Welcome back!”) and of course there is a simple way of doing this:
<?php if ( is_user_logged_in() ) echo 'Welcome back!';?>
That bit of code will allow you to do something like this which will allow you to show the logged in user’s preferred user name (selected in the User preferences), and then a list of member only pages (private page parent is page ID 20 – I am showing all sub pages of the members-only pages marked as private). If the user is not logged in, they will get a Welcome visitor! greeting :
<?php if ( is_user_logged_in() ) : global $current_user; get_currentuserinfo(); ?> <p>Welcome back <?php echo esc_html( $current_user->display_name );?></p> <p>Here is a list of private pages only viewable by Members:</p> <ul> <?php wp_list_pages('post_status=publish,private&child_of=20');?> </ul> <?php else : ?> <p>Welcome, visitor!</p> <?php endif;?>
You can make it as simple as a change in greeting for members and non-members, or put in specific logic like I did with showing the pages. The potential is limitless.
you are awesome, exactly what I was looking for
Thanks for this, just what I was looking for. Kepp up the good work 🙂
Thank you very much for this code. I’m not very code savvy so I was searching for quite a while to find some wordpress php code that would do exactly this. *thumbs up* =)
Great job. Thank you. Much needed.