Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm using <?php print $name; ?> in node--front.tpl.php for an exceptional case, where I can't use Views and the output goodies Views has to offer. By default $name output is themed to link to author profile.

Is there anyway to output the author name in raw text, without going throught a preprocess function?

share|improve this question
    
this is not a real answer, but you could: <?php strip_tags($name); ?> –  jdu Aug 7 '12 at 19:09
add comment

1 Answer 1

up vote 4 down vote accepted

The entire node object is available as $node so you should be able to do something like:

$author=user_load($node->uid);
print check_plain($author->name); // reasonably safe given the 'Spaces are allowed; punctuation is not allowed except for periods, hyphens, apostrophes, and underscores.' validation upon registration but let's check_plain() it anyway

or

$author=user_load($node->uid);
print check_plain(format_username($author));

if you have modules installed that modify the display of the user name.

share|improve this answer
2  
Note that you should a) use format_username($author) to give modules like realname a chance to do something to the username and b) use check_plain() when printing it as it might contain unsafe text. –  Berdir Aug 7 '12 at 19:56
1  
You can't rely on the validation because usernames might have been created through other ways like rules or imports. And when using realname.module and format_username(), the displayed realname probably comes directly from fields, which most likely aren't validated. –  Berdir Aug 7 '12 at 20:35
    
Thanks @Jimajamma. Working as expected. Berdir I tried integrating both a) and b) suggestions but since I'm not really a php coder, I'm having some troubles with format_username (likely a syntax issue). Here's the code <?php $author=user_load($node->uid); print check_plain(format_username($author->name)); ?> –  Tunox Aug 7 '12 at 20:35
    
ah allright so it's (format_username($author)) and not (format_username($author->name)) Thanks again Jimajamma :) I don't have any particular modules that toy around with usernames and anyway this is an excpetion only for the homepage, but gonna use the 2nd solution for future safeguard. Don't know who might handle this website in 1 or 2 years from now. –  Tunox Aug 7 '12 at 20:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.