Mike Waller
asked on
Getting Error on Link Using PHP
I have the following but the Logout link is wrong in the browser:
<?php
if ( is_user_logged_in() ) {
echo '<a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Logout</a>' ;}
else {
echo '<a title="Login" href="/wp-login.php">Login </a>';}
?>
How should rewrite this code so that the Logout link works?
<?php
if ( is_user_logged_in() ) {
echo '<a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Logout</a>'
else {
echo '<a title="Login" href="/wp-login.php">Login
?>
How should rewrite this code so that the Logout link works?
ASKER
I tried your example at top but now the site is broken and is blank white:
<?php
if ( is_user_logged_in() ) {
echo '<a href="<?php echo wp_logout_url( 'http://www.mydomain.com'); ?>" title="Logout">Logout</a>' ;}
else {
echo '<a title="Login" href="/wp-login.php">Login </a>';}
?>
It appears that maybe it could an issue with the single or double quotes?
It's a WordPress site.
<?php
if ( is_user_logged_in() ) {
echo '<a href="<?php echo wp_logout_url( 'http://www.mydomain.com'); ?>" title="Logout">Logout</a>'
else {
echo '<a title="Login" href="/wp-login.php">Login
?>
It appears that maybe it could an issue with the single or double quotes?
It's a WordPress site.
ASKER
btw, I'm placing this in the header.php page.
ASKER
I have in there the following:
<?php
if ( is_user_logged_in() ) {
echo '<a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout">Logout</a>' ; }
else {
echo '<a title="Login" href="/wp-login.php">Login </a>';}
?>
However, in the brower, the php code is converted to a string. How do I fix the above so that the php code is not treated as a string?
<?php
if ( is_user_logged_in() ) {
echo '<a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout">Logout</a>'
else {
echo '<a title="Login" href="/wp-login.php">Login
?>
However, in the brower, the php code is converted to a string. How do I fix the above so that the php code is not treated as a string?
Consider turning the error reporting, error display and error logs on. This should enable you to see the error messages.
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
Did you try the other example?
echo '<a href="<?php echo wp_logout_url($redirect = 'http://www.yourwebsite.com'); ?>" title="Logout">Logout</a>' ;}
echo '<a href="<?php echo wp_logout_url($redirect = 'http://www.yourwebsite.com'); ?>" title="Logout">Logout</a>'
Oh right, the echo statements are using single quotes. You might have to put backslashes in front of them or try double quotes, or the second suggestion.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay, that worked, thanks!
echo '<a href="<?php echo wp_logout_url( 'http://www.yourwebsite.com'); ?>" title="Logout">Logout</a>'
or
echo '<a href="<?php echo wp_logout_url($redirect = 'http://www.yourwebsite.com'); ?>" title="Logout">Logout</a>'
Wordpress uses this function to log out, which I think you know.
function wp_logout_url($redirect = '') {
$args = array( 'action' => 'logout' );
if ( !empty($redirect) ) {
$args['redirect_to'] = $redirect;
}
$logout_url = add_query_arg($args, site_url('wp-login.php', 'login'));
$logout_url = wp_nonce_url( $logout_url, 'log-out' );
return apply_filters('logout_url'
}