Link to home
Start Free TrialLog in
Avatar of Mike Waller
Mike WallerFlag for United States of America

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?
Avatar of nanharbison
nanharbison
Flag of United States of America image

Try just putting the home page:


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', $logout_url, $redirect);
}
Avatar of Mike Waller

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.
btw, I'm placing this in the header.php page.
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?
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);
Did you try the other example?
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
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, that worked, thanks!