Jan
29
2009

Issue with the User Logout Button in WordPress 2.7

by   |  Posted in Tutorials  |  9 comments

I noticed a small issue in WordPress 2.7 when I was designing a login bar at the top of a client’s site and the logout link would not log the user out. I had coded the same way I had coded a logout button in the past, but it wouldn’t function properly. That is because WordPress 2.7 uses a new function to log a user out.

Here is the code I use to use:

<a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout">Logout</a>

And here is the new code I now use:

<a href="<?php echo wp_logout_url() ?>">Logout</a>

And here is a little bit of spice to make it redirect back to the current page:

<a href="<?php echo wp_logout_url(get_permalink()); ?>" >Logout</a>

Nore: If you are using an theme that was designed before 2.7, you should go into your comments.php file and change the anchor tag around line 78 to fix your Logout link.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , , , , ,

Short URL: http://bit.ly/dii4S6

Discussion 9 Comments

  1. jeff on March 14, 2009 at 2:52 am

    hello i aslo have a problem with logout when using the wordpress 2.7 in my site..

    when i click logout it takes me to a page where it shows the msg

    “You are attempting to log out of this site

    Please try again.”

    with “wp-login.php?action=logout&redirect_to=http://www.abc.in”

    at the top navigation bar…

    and when i click on “Please try again” which is a link i get redireted to the home page…

    can u help me with this …cuz am new to using wordpress….

    • c.bavota on March 16, 2009 at 5:30 pm

      Hey Jeff,

      I would have to take a look at your site. What is your URL?

  2. Tomax on April 28, 2009 at 3:07 pm

    This is going to seem dumb … but where do I drop that code? (which template)?

    I would LIKE it to just be in the Links section (where the default is “Blogroll”, but if not possible, then either somewhere on the sidebar or toward the top of the screen.

    The data on the site is of a highly personal nature, so I don’t want the 1 other person who has access to accidentally leave it open for anyone walking by! grin

    THANKS!

    T

  3. Mahmood Yaqub on January 26, 2011 at 11:16 am

    Bavota.. Awesome job my friend… I browsed 100′s of sites with so many explanations.. your post was a quick fix. I wish i read this post the first. Keep posting!!!

  4. Thea on May 3, 2011 at 2:33 am

    I agree with Mahmood.
    You explain it very clearly and direct to what you really wanted to imply.I don’t personally used WordPress 2.7, but my colleagues are the one who used this most of the time.I also heard them like this problem and i think they also made a way to fix the problem.Well anyway I’ll tell them about this.

  5. Jamie Roux on May 12, 2011 at 12:17 pm

    Hi there. Will this work on the latest WordPress version 3.1?

    • c.bavota on May 13, 2011 at 10:14 am

      Yes. That function still works in the latest release of WordPress.

  6. Dave on May 27, 2011 at 8:29 am

    Thank you! Life just got simpler. Quick question. On wordpress and plug-in updates I am spending large ammounts of time saving data prior to installing the update per the directions. Is this necessary? Is there a faster way?

    • c.bavota on May 27, 2011 at 5:46 pm

      If you have an automated backup setup then the backup portion would be done for you.