wp_logout_url()WP 2.7.0

Retrieves the logout URL. Returns the URL that allows the authorized user to log out of the site.

The result is passed through esc_html().

Used By: wp_loginout()
Hooks from the function

Return

String. The logout URL. Note: HTML-encoded via esc_html() in wp_nonce_url().

Usage

wp_logout_url( $redirect );
$redirect(string)
Path to redirect to after the logout.
Default: ''

Examples

0

#1 Get the logout URL in any part of the theme

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

#2 Go to the home page after the logout

<a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Logout</a>
0

#3 Go to another site after the logout

Suppose you want the user to go to another site after the logout (not necessary the site of the current multisite network). To do that, you need to add the site to the whitelist using allowed_redirect_hosts filter.

<?php
add_filter('allowed_redirect_hosts','allowed_redirect_site');
function allowed_redirect_site( $allowed ){
	$allowed[] = 'myredirectsite.com';
	return $allowed;
}
?>

<a href="<?php echo wp_logout_url( 'http://example.com' ); ?>">Logout</a>
0

#4 Stay on the current page after system logout

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

#5 Log out and stay on the current page:

$linkpage = esc_url( 'https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
echo sprintf( '<a class="exit" href="%s">Exit</a>', wp_logout_url( $linkpage );

Changelog

Since 2.7.0 Introduced.

wp_logout_url() code WP 6.7.2

function wp_logout_url( $redirect = '' ) {
	$args = array();
	if ( ! empty( $redirect ) ) {
		$args['redirect_to'] = urlencode( $redirect );
	}

	$logout_url = add_query_arg( $args, site_url( 'wp-login.php?action=logout', 'login' ) );
	$logout_url = wp_nonce_url( $logout_url, 'log-out' );

	/**
	 * Filters the logout URL.
	 *
	 * @since 2.8.0
	 *
	 * @param string $logout_url The HTML-encoded logout URL.
	 * @param string $redirect   Path to redirect to on logout.
	 */
	return apply_filters( 'logout_url', $logout_url, $redirect );
}