• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 924
  • Last Modified:

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?
0
COwebmaster
Asked:
COwebmaster
1 Solution
 
nanharbisonCommented:
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);
}
0
 
COwebmasterAuthor Commented:
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.
0
 
COwebmasterAuthor Commented:
btw, I'm placing this in the header.php page.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
COwebmasterAuthor Commented:
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?
0
 
Ray PaseurCommented:
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);
0
 
nanharbisonCommented:
Did you try the other example?
echo '<a href="<?php echo wp_logout_url($redirect = 'http://www.yourwebsite.com'); ?>" title="Logout">Logout</a>';}
0
 
nanharbisonCommented:
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.
0
 
Jason C. LevineNo oneCommented:
<?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>';}

?>

Open in new window


You have a <?php ?> block nested in another block.  That should cause all sorts of errors.

Try this:

<?php

if ( is_user_logged_in() ) {
	echo '<a href="'. wp_logout_url( get_permalink() ). '" title="Logout">Logout</a>'; 
} else {
	echo '<a title="Login" href="/wp-login.php">Login</a>';
}

?>

Open in new window

0
 
COwebmasterAuthor Commented:
Okay, that worked, thanks!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now