I can’t tell you how many times when working in wordpress I have wanted to show or hide elements on the page based on the user being logged in or not. This handy little snippet is a good reference to use for just that purpose.
if ( is_user_logged_in() ) {
echo "You are logged in";
}
else {
echo "You must be logged in to view this.";
}
For those who may not be as advanced with php, I will break it down.
You will want to put the contents of what you need in between the quotes just after echo… For example.
echo "You are logged in"
You would want to only replace the text, You are logged in.
You could also do something more advanced such as show a custom field from a post if the user is logged in. In this case we will show the clients email on a post, and if you are not logged in you cannot view it. This is the method we use on our Design Approval System wordpress plugin for the Project board.
if ( is_user_logged_in() ) {
echo get_post_meta($post->ID, 'custom_clients_email', true); }
else {
echo "You must be logged in to view this.";
}