I'm working on a page on a site but got stuck at this point I want to show the logged in members username/first name of the site content for that I'm using this code

<?php if ( is_user_logged_in() ) { ?>
    <!-- text that logged in users will see -->
<?php global $current_user; get_currentuserinfo(); ?>
<h5>Hi <?php echo $current_user->user_firstname; ?></h5>
<p>You have access to all of the free members stuff, enjoy :) <a href="http://example.com/wp-login.php?action=logout"> Logout </a></p>
<?php } ?>

but instead of showing the username i shows broken code like this See Screenshot. I'm creating the page with WPBakery.

  • @fuxia that answer didn't work for me – Yasir Rehman May 12 at 10:30
  • What happens when you replace <h5>Hi <?php echo $current_user->user_firstname; ?></h5> with <h5>Hi <?php echo $current_user->display_name; ?></h5> or with <h5>Hi <?php echo "Foobar"; ?></h5>? Still weird output? Then something else in your code somewhere else is broken. – leymannx May 12 at 11:27
  • i see this i.imgur.com/JwPooHw.png – Yasir Rehman May 12 at 11:28
  • Yes, I got that. It's the same screenshot as in your question. But what happens when you replace it with the code I suggested? – leymannx May 12 at 11:30
  • nothing changes – Yasir Rehman May 12 at 11:30

It looks like $current_user->user_firstname contains some tag content.

I would replace that line with:

<h5>Hi <?php echo esc_html($current_user->user_firstname); ?></h5>


<h5>Hi <?php echo strip_tags($current_user->user_firstname); ?></h5>

One of those (or either) may solve your problem outright.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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