WordPress at a glance

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().

Is the basis for: wp_loginout()
Hooks in 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

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

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

#2 Stay on the current page after the logout

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

#3 Go to the home page after the logout

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

#4 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>

Code of wp logout url: wp-includes/general-template.php WP 5.2.1

<?php
function wp_logout_url( $redirect = '' ) {
	$args = array( 'action' => 'logout' );
	if ( ! empty( $redirect ) ) {
		$args['redirect_to'] = urlencode( $redirect );
	}

	$logout_url = add_query_arg( $args, site_url( 'wp-login.php', '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 );
}

Related Functions

From category: Login/Logout

More from Template Tags: Main Functions

No comments
    Hello, !     Log In . Register