"Welcome, User" / "Login Register" block for Drupal - Pretty simple but darn userful

Mon, 2012-02-27 08:47

Nowadays in most sites, you can see a text that says, "Welcome, [user-name]", when you are loged in to the site. How we can do that in Drupal ?
Pretty simple!

There are plenty of posts in drupal.org asking for this simple feature but none of them goes "common". Not every site has public registrations on, and sometimes it's not a good idea to redirect user to their account instead of what they were looking at.
So, here we go a little advanced with t() , drupal_get_destination() and extra check to make sure public registrations are enabled before showing a link to register on the site.

Just put this code in a block with input format set to PHP (Enable PHP Filter module to enable PHP input format), in tpl.php files or anywhere you want (in a Drupal site - duh!)

Use the code that suits your Drupal version. This block will give you,

  • If the user is logged in,
  • Welcome, Foo | Logout
    Foo will be the username of the account, and both Foo and Logout are linked to user's profile and logout URL.

  • If the user is not logged in,
  • Login | Register
    Login and Register will be linked to login and register pages, and with "destination" URL parameter set, so users will be redirected back to where they were when clicking the link.

Drupal 6 code

global $user;
if (
$user->uid) {
t('Welcome, !username | !logout', array(
'!username' => theme('username', $user),
'!logout' => theme('username', $user),
else {
$destination = drupal_get_destination();
l(t('Login'),'user/login', array(
'query' => array(
'destination' =>  $destination,

// Print Register link only if allowed to do so.
if (variable_get('user_register', 1)) {
' | ';
l(t('Register'),'user/register', array(
'query' => array(
'destination' =>  $destination,

Drupal 7 code

global $user;
if (
$user->uid) {
t('Welcome, !username | !logout', array(
'!username' => theme('username', array('account' => $user)),
'!logout' => l(t('Logout'), 'user/logout'),
else {
$dest = drupal_get_destination();
l(t('Login'),'user/login', array(
'query' => $dest,

// Print Register link only if allowed to do so.
if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
' | ';
l(t('Register'),'user/register', array(
'query' => $dest,

Update: I have updated the code to use theme_username so other modules such as RealName can alter the username with a different text.
Thank you very much rooby for pointing that out.


This is just a guess, but as somebody who is a nwbiee to using CMS, I was asking myself a similar question: I want to use Drupal (or some other CMS) to maintain a site that until now has consisted mainly of static web pages. Visitors of the site do not and should not be able to log in nor should they see any log-in form. So the first thing I asked myself after I installed Drupal and browsed to the default generated site is: how to remove the login form so that site visitors do not see that one can log in to this site at all.What I really would like though is a way that totally disallows any log in for visitors from the WAN and only allows admin logins from our company-internal LAN.

No matter if some one searches for his necessary thing, thus he/she wants to be available that in detail, thus that thing is maintained over here.

We're a group of volunteers and starting a brand new scheme in our community.
Your website provided us with helpful information to work on. You've performed a formidable process and
our whole neighborhood might be thankful to you.

Pretty nice post. I just stumbled upon your blog and wished to
say that I've really enjoyed browsing your blog posts.
After all I will be subscribing to your feed and I hope
you write again soon!

Pretty component of content. I simply stumbled
upon your weblog and in accession capital to claim
that I get in fact enjoyed account your blog posts.
Any way I'll be subscribing for your feeds and even I achievement you
get admission to constantly fast.

What's up colleagues, nice piece of writing and good arguments commented at this place,
I am truly enjoying by these.

Link exchange is nothing else except it is just placing the other
person's weblog link on your page at proper place and other
person will also do same in support of you.

This is a very good tip especially to those fresh to the blogosphere.
Brief but very accurate info… Thanks for sharing
this one. A must read article!

Valuable info. Lucky me I found your website by accident, and I am stunned
why this accident didn't took place in advance!
I bookmarked it.

Pretty section of content. I just stumbled upon your
website and in accession capital to assert that I
get actually enjoyed account your blog posts. Anyway I'll be subscribing to
your feeds and even I achievement you access consistently fast.

Password recovery speed іs an extremely impoгtant factor үoս sɦould ϲonsider in selecting а RAR Passweord recovery tool.
Ηow to judge self-extracting wɦether thе program contains malicious program.
Уou Һave ɑ couple of mainn choices: Restore frfom tҺe web portal:
log іn tօ ƴour account, navigate tօ the file yօu woulɗ liҝe to retrieve,
then choose how ʏou want to retrieve it.

Dairy products and several teaspoons workout of coconut
milk yogurts. The World Peace Diet. If possible, he added
that women who ve consumed 3 ballerina tea Dieters Drink tea that

Please let me know if you're looking for a article writer for your site.

You have some really great posts and I believe I would be a good asset.
If you ever want to take some of the load off, I'd really like to write some articles for your blog in exchange for
a link back to mine. Please shoot me an e-mail if interested.

I do trust all the ideas you have introduced in your post.
They are very convincing and will certainly work. Nonetheless, the posts
are very short for beginners. May just you please extend them a
bit from next time? Thanks for the post.

What i don't realize is in fact how you're not actually much more well-favored than you may
be now. You're so intelligent. You recognize therefore
considerably relating to this topic, produced me for my part imagine it from numerous numerous angles.
Its like men and women don't seem to be interested except
it is one thing to accomplish with Woman gaga! Your personal stuffs nice.
All the time maintain it up!

What's Taking place i am new to this, I stumbled upon this I have discovered It positively useful and
it has aided me out loads. I hope to contribute & aid
different users like its helped me. Good job.

Hey there I am so thrilled I found your blog page, I really found you by error, while I was browsing on Askjeeve for something else, Regardless I am here now and would just like to say thanks a lot for a fantastic post
and a all round enjoyable blog (I also love the theme/design), I don't have time to look over it all
at the minute but I have bookmarked it and also added in your RSS feeds, so
when I have time I will be back to read much more, Please do keep up the superb job.

What's up, I wish for to subscribe for this web site to obtain newest updates,
so where can i do it please help out.

Useful article , I am going to spend more hours learning about this subject.

Appreciate the recommendation. Will try it out.

I got this website from my pal who shared with me about this web page and at the moment this time I am browsing this web page and reading very informative articles at this place.

Quand vous ajoutez des engrais, souvenez vous que les engrais tiques se décomposent à un ensemble de taux différents.

Running a business hand to mouth with no guarantees of income is
a touchy situation. In most current floor plan designs,
this important area is planned to open into the dining space.

Calgary Custom Homes for example, also checks the
sewer lining and natural gas installation during this process, and it is also the perfect way to start the roofing process, doors,
and windows construction.

You're doing it for far better wellness and even more self-esteem.
both you can obtain from dropping additional weight.

Pagar por contenido … creo que sería muy interesante obtener el dato de qué
tantos usuarios de Spotify en Latam son pagados.

A high quality debt combination solution will certainly make it a leading priority to incorporate all financial obligation and decrease the general interest rate being applied to the monthly pqyment process.

My brother suggested I may like this website. He used to be entirely right.
This put up actually made my day. Youu can not imagine just how a lot time
I had spent for this information! Thank you!

I would recommend that you hold the shisha coal an arm's length away with tongs,
as well as light it with a lighter (held in your various other hand).

When combined with bodywork, making use of essential
oils will considerably improve the massage experience.

Mesotherapy uses numerous shots of pharmaceutical and also homeopathic medications plant essences The
Advantages of Environment-friendly Tea Essence:
Anti-Aging Anti-Cancer Weight Control.

I drop a leave a response when I like a post on a website or I have something
to valuable to contriubute to the conversation. It is a result of the passion communicated in the article I looked at.
And onn this article Add new comment | Ayesh Karunaratne. I was moved enough to drop a thought ;-) I do have a couple
of questions for you if you don't mind. Is it just me or
does it look as if like some of these responses come
across like they are written by brain dead folks? :-P And, if you are writing at additional sites, I'dlike to follow everything fresh
you have to post. Would you list the complete urls
of all your communal sites like your twitter
feed, Facebook page or linkedin profile?

His readings are done utilizing a. mixture of precognition, mediumship and
also clairvoyant understanding.

Perhaps now you are actually having afterthoughts concerning the asserted spiritual abilities of Jeanette Wilson.

We offer the entire L.a room and are close to Burbank and also the San Fernando Valley.

Hurrah! At last I got a weblog from where I be able to truly get valuable
data concerning my study and knowledge.

Hi! This is my first visit to your blog! We are a collection of volunteers and starting a new initiative in a community in the same niche.

Your blog provided us valuable information to work on.
You have done a marvellous job!

I am sure this post has touched all the internet visitors,
its really really fastidious article on building up new website.

Infant botulism is a result of consuming the spores in the botulinum bacteria,
which then grow inside the intestines and release toxin.
For other addicts, outpatient treatment might be sufficient for
overcoming the problem while still allowing anyone to live a semi-normal life.
With countless numbers of diet plans out there promising fantastic weight loss results and all sorts of the hype in regards to the a huge number of "miracle" fat loss pills, powders and drinks, it is no surprise fat loss thinks
quite the challenge.

An intriguing discussion is worth comment. There's no doubt that that you need to write more on this subject,
it may not be a taboo matter but generally people don't discuss such subjects.
To the next! All the best!!

F*ckin' remarkable issues here. I am very happy to see your article.

Thank you a lot and i'm having a look ahead to touch
you. Will you please drop me a mail?

2-liter (available only on SLT and Denali trims) delivers 420 hp and 460 lb.
But, it gets a more than a bit interesting when the comparison is Chevrolet Silverado
to GMC Sierra. There were the side integrated bumper steps, which
I am really pleased about and there is LED lighting in the
bed area.

It is so difficult to find useful information but this
undoubtedly qualifies together. Causing certainly will truly come back soon and happy!

Thank you for some other wonderful article. The place else could anybody geet thnat kind of info
in suuch an ideeal method of writing? I have a presentation next week, and I'm at the look for such info.

If you desire to obtain a great deal from this piece of writing then you have to apply such methods to your won website.

That is a good tip especially to those new to the blogosphere.
Short but very accurate info… Appreciate your sharing this one.
A must read article!

For hottest information you have to pay a visit internet and on internet I found this site as a best web site for hottest

Your means of explaining the whole thing in this article
is in fact pleasant, all be capable of without difficulty understand it, Thanks
a lot.

you're really a excellent webmaster. The site loading pace is incredible.
It seems that you're doling any unique trick. Moreover, The contents are masterpiece.
you have performed a magnificent task in this subject!

This is understandable when the manufacturers are
different such as Ford and Ram comparing to a Chevrolet or GMC.
' It appears that designers and engineers took that to heart in creating the
2014 GMC Sierra pickup truck. There were the side integrated bumper steps, which I am really pleased about and there is LED lighting in the bed area.

So if you want to get more entertainment value from your Nintendo gaming console,
all you need to do is to install the R4 Gold
Upgrade. Add images, graphics and video increase conversion. If you think you have a product that has the qualities to compete internationally on the music market -.


Your thoughts