you havent defined what $user_identity is.
Oh, and how can I do that?
well start by explaining why you chose to use it in the first place? I’m going to guess you didnt pluck it out of the sky, right?
I’ll guess that you saw it in some other code – so look at how they defined it.
I want to show the username of someone who is logged in. I got the code form comments.php:
<?php if ( $user_ID ) : ?>
<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out of this account">Logout »</a></p>
<?php else : ?>
input fields (name, e-mail, website).......
<?php endif; ?>
If I put this code in my header, it shows the input fields, so the code doesn’t see that i’m logged in…. I hope someone can help me.
$user_identity is indeed a global, however, in subsections of themes, like header.php, you don’t necessarily have access to those globals. The comments template gets them for free because, well, it’s the comments template and it tends to use those sort of things. The header, on the other hand, generally doesn’t use those and so it doesn’t get them for free. It’s a security thing.
Outside of the comments template, the correct way to do this sort of thing is like so:
$user = wp_get_current_user();
echo $user->display_name;
Use the display name whenever you want to display the user name.
The is_user_logged_in() is also correct for usage in the header, you don’t get the $user_ID global there either.
Also note that nothing stops you from using these functions in the comments template either. They work perfectly well there and are probably preferable in the long run. The $user_* globals are just a convenience for the comments template.
Hmm okay, but how can I put that to my code (I’m not so good with php 😉 ), if I use this, it still doesnt work:
<?php {
if ( ! is_user_logged_in() )
include (TEMPLATEPATH . '/loginform.php');
else
$link = '
<div class="logged_in"><div class="logged_in_content">Logged in as: <?php $user = wp_get_current_user();
echo $user->display_name; ?>
<a href="' . get_settings('siteurl') . '/wp-login.php?action=logout">Logout</a></div></div>';
echo apply_filters('loginout', $link);
}
?>
Ahh, I see, you’re trying to assign a link. Slightly different, that.
Try this:
<?php {
if ( ! is_user_logged_in() ) {
include (TEMPLATEPATH . '/loginform.php');
} else {
$user = wp_get_current_user();
$link = '<div class="logged_in"><div class="logged_in_content">Logged in as:'.$user->display_name.'<a href="' . get_settings('siteurl') . '/wp-login.php?action=logout">Logout</a></div></div>';
echo apply_filters('loginout', $link);
}
}
?>
Ah, thanks man! That works perfect.