If you want to do this in a module (recommended as opposed to adding php code to a block, which would then not be in version control), then you can do this:
(in this case, all this code would go in a custom module named userwelcome.)
/**
* @file
* Adds a block that welcomes users when they log in.
*/
/**
* Implements hook_theme().
*/
function userwelcome_theme($existing, $type, $theme, $path) {
return array(
'userwelcome_welcome_block' => array(
'variables' => array('user' => NULL),
),
);
}
/**
* Implements hook_block_info().
*/
function userwelcome_block_info() {
// This example comes from node.module.
$blocks['welcome'] = array(
'info' => t('User welcome'),
'cache' => DRUPAL_CACHE_PER_USER,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function userwelcome_block_view($delta = '') {
global $user;
$block = array();
switch ($delta) {
case 'welcome':
// Don't show for anonymous users.
if ($user->uid) {
$block['subject'] = '';
$block['content'] = array(
'#theme' => 'userwelcome_welcome_block',
'#user' => $user,
);
}
break;
}
return $block;
}
/**
* Theme the user welcome block for a given user.
*/
function theme_userwelcome_welcome_block($variables) {
$user = $variables['user'];
$output = t('Welcome !username', array('!username' => theme('username', array('account' => $user))));
return $output;
}
If you then wanted to override the themeing of this block in a theme you would do this (in your template.php file of your theme):
/**
* Theme the userwelcome block.
*/
function THEMENAME_userwelcome_welcome_block(&$variables) {
// Return the output of the block here.
}
Note that because this is a custom module you could also just update the theme funciton in the module directly.
If you don't want to use a custom module you can create a custom block with php code and add this:
global $user;
// Only for logged in users.
if ($user->uid) {
print 'Welcome ' . theme('username', array('account' => $user));
}