0

I have a PHP file in Wordpress that has some HTML, "Login & Register" that I would only like to show when a member of the site is not logged in. When they are logged in I would like it to show "Welcome, [name]."

I'm just not sure how to use HTML in a PHP If statement. The condition is this:

<?php
      if ( member_is_logged_in() ) {
         print "Welcome [name]";
      } else {
         print "<a href="...">Login</a> | <a href="...">Register</a>";
      }
?>

That's how I think it should look, but it's not working. I'm not that great with PHP, but I would appreciate any help. Thanks!

3
  • 2
    how is it not working, no output? not detecting logged in state ? Commented Aug 9, 2012 at 3:34
  • Well Sacheleen and Gordan provided excelent answers.. another solution is your choice of quotes. If you wrap your string in double quotes and have a double quote in the string, you break your string, so you can escape it by add a ` before it like \"` and this logic also applies to single quotes. Commented Aug 9, 2012 at 3:35
  • Thanks for everyone's help! I got it figured out :) Commented Aug 9, 2012 at 3:57

4 Answers 4

3

You can use PHP variables in double quoted strings. so just replace [name] with $name or whatever the variable is. Note this will not work with single quotes.

Also, you have two sets of double quotes in the else case. This will result in a syntax error. Make one of them single quotes or escape the inside quotes.

<?php
      if ( member_is_logged_in() ) {
         print "Welcome $name";
      } else {
         print '<a href="...">Login</a> | <a href="...">Register</a>';
      }
?>

You can also just end the PHP tag and write the HTML directly.

<?php
      if ( member_is_logged_in() ) {
         print "Welcome $name";
      } else {
?>
         <a href="...">Login</a> | <a href="...">Register</a>
<?php
      }
?>

There exists a better and easier to read syntax for this:

<?php if(<your evaluation here>): ?>
HTML for when true
<?php else: ?>
HTML for when false
<?php endif; ?>

Check out Alternative syntax for control structures for more information on this.

And while we're at it, print vs echo, which one is faster?

Sign up to request clarification or add additional context in comments.

2 Comments

I know this is a matter of preference, but I find mixed PHP and HTML in the way you describe in the "better" syntax harder to read. That 2nd syntax is just horrible - not just mixed PHP-and-HTML but mixed PHP and mixed-PHP-and-HTML.
Just presenting the available options. :) Personally, I kind of like option 3, but I agree that 2 sucks.
1

simply:

<?php
      if ( member_is_logged_in() ) {
         print "Welcome $name";
      } else { ?>
         <a href="...">Login</a> | <a href="...">Register</a>
      <?php }
?>

Comments

1

If you can use wordpress try below code:

See Utl:- http://codex.wordpress.org/Function_Reference/wp_get_current_user

E.g.: To determine if there is a user currently logged in, do this:

<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
} else {
    // Logged in.
}
?>

Comments

0

Try this code chunk and let me know---

<?php
      if ( member_is_logged_in() ) {
    global $display_name;
    get_currentuserinfo();
         print "Welcome ".$display_name;
      } else {
         print "<a href="...">Login</a> | <a href="...">Register</a>";
      }
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.