Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By jessmagz on
Hi everyone!
I am using the Front Page Module to customize my front page - one for Guest, and a separate front page for Authenticated users.
I want to display a line saying:
Welcome !
for my authenticated users. How can I achieve this in PHP coding? I don't know the variable to call that will give me the "current logged in user".
I tried
print $user
and print $username
it did not work.
Please help. Thanks!
Jess
Comments
Welcome <username> ...
check this out: http://drupal.org/project/token
I found this some time ago
I found this some time ago on Drupal, but can't remember where exactly:
Cheers,
Carey
It didn't work...
Hi,
Thanks for your reply. But the code you have given didn't work. It just displayed the word "Welcome".
I can't figure out why. Thanks.
All of it...
It worked!
A big thanks! It worked at last! I spent the whole afternoon searching... huhuhu!
Thanks...
global $user;
is a powerful code :-)Jess
:)
You're welcome. Glad I could help.
whats the "if" if a user is
whats the "if" if a user is not registered logged in + how to show than a register/login?
Maybe this
Maybe this is what you are wanting:
great, thanks a lot!
great, thanks a lot!
Put welcome text on top of the header
Hi,
I want to do almost the same thing
I don't want to show the user id on navigation menu but on top of header, where should I put this code?
EDIT: You shouldn't turn on
EDIT: You shouldn't turn on the php filter module, but that could get it to work.
Otherwise a little module that uses some of the php posted in this thread will work awesomely. The problem we ran into was that this is not satisfactory for multi-lingual sites, but you can simply have a case on lang. Something like this:
how would I print
how would I print profile_name? I tried calling $account globally, but that didn't work.
subsribing
subsribing
Try this:
username intent of my account
i want to show user name in place of my account in user menu.
and please tell me that this code where i past or write
How would I print this on a
How would I print this on a seperate page?
like if i wanted to create a seperate PHP file that just sets the currently logged in user to a parameter so you can print that parameter throughout the PHP file.
See above comment
See above comment http://drupal.org/node/213633#comment-5761898 and set the block to show only on that page. Or create a page-specific template in the theme for the page where you want it to appear, and add the code there.
I do not understand when you say
You could make a module to do the job, but why would you want to?
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
I would like to add more
I would like to add more field, for example: last_name, however I can't display it
for user name in place of my account in user menu
can any one tell me that this code where past for show user name when user loged in.
global $user;
Welcome
print l($user->name,'user/'.$user->uid);
or simply i want to show user name
Summary for D7
I'd like to sum this up. The best way that I found was to make the following changes.
Notice the
user/
before the logout and register. At least with D7, you need to have the "user/" in front of logout, register, etc.Ain't it great....!
Please include file names
It would be really helpful if people could add WHERE the code goes (what file) in their responses for those of us who are new to Drupal. This would really save time and make people more productive sooner.
Thanks for all the great input.
This goes in page.tpl.php in
This goes in page.tpl.php in the theme. Or in an alternative page template specific to the relevant page (see https://drupal.org/node/1089656). Or in some other template which is printing content, as explained on that linked page.
You have to work out the part of page.tpl.php or other template which is responsible in your theme for outputting the part of the page where you want to add something, and place it in that file.
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
Use Views to do this!
Forget all of the code snippets, you can do this with the Views module! The result is clean, elegant, flexible, and limitless.
Structure > Views > Add new view
Show: Users sorted by Unsorted
Don't create page
Create block: no title
Display format: Unformatted list of Fields
Items per page: blank
Don't use pager
Continue & edit
Change the following:
BLOCK SETTINGS
Block name:None
Access:Role | authenticated user
Advanced (expand on the right)
CONTEXTUAL FILTERS
Add
User: Uid
WHEN THE FILTER VALUE IS NOT AVAILABLE
Provide default value
Type
User ID from logged in user
WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED
Override title
%1
Edit the field User:Name
REWRITE RESULTS
Rewrite the output of this field
Text: Welcome back, [name].
Save the view
Go to Structure > Blocks > View:[your view] > assign it to a region.
This gives you a block with the default title set as the username of the logged-in user, and shows a message "Welcome back, [username]"
Of course if you know views you know that you can configure the fields to display ANY user-field, like a photo, real name, birthday, last logged in, etc. etc. etc.
Really this is the swiss army knife of all user-specific block needs.
Attached a view export of my example above. Let me know what you think of this!
- lunk_rat
lunk_rat is Acquia Certified Grand Master
Views is cool!
Works great. Thank you.
Stan B
Worked for me.
Hi, I've only been using Drupal for a couple of weeks. Found your post very helpful. Hope all the rest of my setting up tasks go as easy as this. Thanks John
Nice idea, the View works
Nice idea, the View works well in Views 3.x!
If you're on D6 or Pressflow, just change
$view->core = 7;
to$view->core = 6;
in the export, and Views will import it cleanly.Thank you!!
This post answered my question and saved me a ton of time - thank you! The imported Lunk_rat View was a great starting point that I edited to meet my needs.
Very much appreciated.
Ditto!
Huge help, lunk_rat, thanks!