Saturday, February 06, 2010

Display different content for non-registered users and for registered users on Wordpress

I was looking up information as to how can I make sure that a blog displays different content for non-registered users and again a completly other content for registered users, but honestly I couldn't find any article or thread that explained it very well or simply missed the non-registered users part.

The code I'm going to provide you with below is an example to how I resolved this (very simple actually).

Some explanation to it before you start using it:
1 The function <?php wp_register('', ''); ?> is actually an anchor that is called to display 'Register' or 'Site Admin' depending on whether or not you're logged in. Of course as we call this function only at the non-registered users part, it will only display 'Register', just as we want it (more explanation to this command can be found in WordPress's Codext article Function Reference/wp register).

2 For the case someone visits you who already registered but hasn't logged on yet this time of their visit, you can add the following anchor to give them the possibility to logon: <a href="<?php echo wp_login_url(); ?>" title="Logon to your account at <?php bloginfo('name'); ?>">Logon</a>»</p>

3 The function <?php else : ?> is actually deviding the 2 parts. Anything that is before this line will be called in case a non-registered user visits your blog, anything that is placed after this line will be displayed for already registered users.

4 Then in the registered users part we can display a fancy line that shows something like "hello [user]" or "you're logged in as [user]" where the [user] part is replaced by the following anchor: <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php global $current_user; get_currentuserinfo(); echo $current_user->user_login;?></a> - if clicked, the user gets into their wordpress profile on your blog (important note: you may have a wordpress blog already and therefore a wordpress account with logon name and password, but each blog has their own registration process, therefore, if you register to another blog that you didn't create, you will see your profile for this blog only clicking the above link, not your own 'admin' wordpress profile). Then the other part of fanciness is a logout link that displays by using the following anchor: <a href="<?php echo wp_logout_url(); ?>">Logout &raquo;</a>

Below you'll see an example you can put into one of your WordPress pages (suggest to put it into header.php, that's where these things usually go). Of course you can change the different styles (like the class targettedtext, logon, logout...) according to your needs:
(click in the box to highlight the code for copy)

No comments:

Post a comment

Dear legitimate commenters, please post your comments below. I'd be happy to receive your feedback; helps me a lot improving, or getting reassurance that I'm doing something really great here.

IMPORTANT! A note to commenters planning on advertising their business in comments: please be informed that I'm NOT going to post your comments but will ALWAYS mark them as SPAM.