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!
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.
You can also just end the PHP tag and write the HTML directly.
There exists a better and easier to read syntax for this:
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?