Hi everyone and Good day!Am new to php and I have a problem with the code below.

<?php if (is_user_logged_in() ) //only logged in user can see this 'echo'<href="http://localhost/itsz.com/profile">Profile</a>';
?>

What i want it to do is to only show the link "profile" 'only' to logged in users but it doesen't. Instead, profile link is shown to both loggedin and non logged in users. i know i have made an error in the code syntax but i have no idea where. It was just a copy and paste game and edit a little.Php coders help me. Thank you.

Dani AI

Generated

Good catch by — the anchor ended up outside the conditional because of misplaced quotes/comments, so the browser always saw HTML and printed the link. PHP only controls output that is inside a running PHP block; anything accidentally left as raw HTML will be visible to every visitor.

A safe, clear pattern (keeps PHP and HTML separate, escapes the URL) is this:

<?php if ( is_user_logged_in() ) : ?>
<a href="<?php echo esc_url( home_url( '/profile' ) ); ?>">Profile</a>
<?php endif; ?>

This prints only the word "Profile" as a link for logged-in users. If you prefer inline PHP, use braces and build the string rather than breaking quotes in the middle — that avoids the kind of syntax problem that triggered the original bug.

If the link still appears for guests, check these common causes:

  • View the page in a private/incognito window to rule out your session.
  • Clear any full-page cache (WP caching plugins, server cache, CDN) — cached HTML can show logged-in content to everyone.
  • Make sure the code runs after WordPress has loaded (put it in a theme template or functions hooked to the right action). If you put PHP into a plain text widget without a handler, it won’t execute and the HTML will be output as-is.

Best practice: keep conditionals obvious (braces or colon/endif), escape output (esc_url/esc_html), and avoid linking to admin-only endpoints without proper capability checks. This keeps the UI correct and reduces surprises like the one ran into.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

Get rid of the single quotes around echo:

<?php if (is_user_logged_in() )
    echo '<href="http://localhost/itsz.com/profile">Profile</a>';
?>

However if it's showing in all cases, it means that is_user_logged_in() isn't working properly. For that we'd need to see the underlying code as we don't have crystal balls.

Wow! its now working like magic! Cant believe those damp '' troubled me for weeks. I need some basic php tutorials.Thanks and May God Bless youuu...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.