PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Hidden Content for Registered Users

Posted by on 18th Jan 2009 WordPress Coding & Design 1 comment

It can be quite useful to be able to add in links or information that you want either yourself, as an administrator, or your registered users to see, on the front end of your website.

The default template comes with user login/logout link, usually under the Meta Information section, but most people prefer to remove this as they don’t want login links to the general public. However, once you’ve logged in, why not give yourself a section of shortcut links to areas within your admin, or even display additional information or widgets, that you don’t want your visitors seeing?

If you’re the only one who logs into your site, or you want the hidden content available to every logged in user, then this is easily achievable using the is_user_logged_in() condition. All you need to do is use this with an if statement, and surround anything you want displayed to logged in users, for example, perhaps a link to the Admin, and a link to the Write Post page:

[sourcecode language="php"]< ?php
if (is_user_logged_in()) :
?>

< ?php endif; ?>[/sourcecode]

You can place any HTML, or PHP code within the if statement, and anyone who is logged in will see it.

Multi Author Blogs

Whilst the above will show content to all logged in users, you may want to have additional information displayed just for yourself. You can achieve this by checking the Username of the logged in user, and comparing it against your own. To do this we can use

[sourcecode language="php"]< ?php
if (is_user_logged_in()) :
global $current_user;
get_currentuserinfo();

if ($current_user->user_login == ‘admin’) :
// put any links or content just for the admin to see
endif;
endif;[/sourcecode]

The above code first checks if the user is logged in, if they are then it gets their current user information, and compares their user login to the username of ‘admin’. You can change the username to your own username, if different.

Either of these methods can be used for shortcut links to your admin, displaying information for your eyes only, or displaying specific content to your registered users.

A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

1 comment - Leave a reply