Get wp_logout_url() To Redirect To Current Page on Any Page, Even Archives

Usually, wp_logout_url() is used to make a Log Out link. If you’re also passing get_permalink() as a parameter in order to stay on the same page after logging out, you may have something that looks like this:

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

But that doesn’t work to stay on the same page on category pages, tag pages, other taxonomy and/or archives pages, or on the blog page. To fix that, instead of using just get_permalink(), use this:

// Get permalink on any page

if ( is_tax() ) {
	$permalink = get_term_link( get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
} elseif ( is_post_type_archive() ) {
	$permalink = get_post_type_archive_link( get_query_var('post_type') );
} elseif ( is_home() ) {
	$permalink = get_permalink( get_option( 'page_for_posts' ) );
} else {
	$permalink = get_permalink();
}

// Better logout link

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

That link will properly let a user log out and stay on the same page, even on archives pages or the main blog posts page.

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]