Proper way to display logout link in WordPress

Recently, I was debugging an issue with logout for a friend who was running a WordPress blog. His theme had a logout button in the sidebar, which stopped working after he upgraded to the latest version of WordPress from a pretty old version.

After some debugging, I found that the nonce value was missing from the logout link. In older versions of WordPress, the nonce value was needed, but in newer versions, we need to pass the nonce value to perform the logout.

wp_logout_url

We need not append the nonce value manually, instead we can use build-in function wp_logout_url(). This function will automatically append the nonce value to the logout link.

If you want the user to be redirected to the current page he is viewing, after logging out, you can use the following code snippet.

<a href="<?php echo wp_logout_url();?>&redirect_to=<?php echo $_SERVER['REQUEST_URI']; ?>"><?php _e('Logout'); ?></a>

Related posts

Tags: ,

0 Comments so far

Follow up comments through RSS Feed | Post a comment

1 Tweetbacks so far

Leave a Reply

Your email address will not be published. Required fields are marked *