• About WordPress
    • About WordPress
    • WordPress.org
    • Documentation
    • Support
    • Feedback
  • Log In
  • Register
WordPress.org
WordPress.org
  • Plugins
  • Themes
  • Patterns
  • Learn
  • Support
    • Documentation
    • Forums
  • News
  • About
  • Get Involved
    • Five for the Future
  • Showcase
  • Mobile
  • Hosting
  • Openverse
  • Get WordPress
Get WordPress

Code Reference

Skip to content
Filter by type:
Search
Browse: Home / Reference / Hooks / logout_redirect

apply_filters( 'logout_redirect', string $redirect_to, string $requested_redirect_to, WP_User $user )

Filters the log out redirect URL.

Contents

  • Parameters
  • Source
  • Changelog
  • User Contributed Notes

Parameters

$redirect_to

(string) The redirect destination URL.

$requested_redirect_to

(string) The requested redirect destination URL passed as a parameter.

$user

(WP_User) The WP_User object for the user that's logging out.


Top ↑

Source

File: wp-login.php

View on Trac


Top ↑

Changelog

Changelog
Version Description
4.2.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note
    Contributed by Gabor Lippert — 6 months ago

    Logout function (that actually works) according user role:

    function wpdocs_logout_redirect( $redirect_to, $requested_redirect_to, $user ) {
    
        $user_roles = $user->roles;
        $user_has_admin_role = in_array( 'administrator', $user_roles );
    
    	if ( $user_has_admin_role ) :
    		$redirect_to = admin_url();
    	else:
    		$redirect_to = home_url();
    	endif;
    
    	return $redirect_to;
    }         
    add_filter( 'logout_redirect', 'wpdocs_logout_redirect', 9999, 3 );
    

    Expand full source codeCollapse full source code

    Log in to add feedback
  2. Skip to note 2 content
    You must log in to vote on the helpfulness of this noteVote results for this note: -4You must log in to vote on the helpfulness of this note
    Contributed by mikelast — 3 years ago

    taken from the article at sourcexpress.com/logout_redirect-filter-hook-introduced-in-wordpress-4-2

    This filter comes with 3 parameters, string $redirect_to The redirect destination URL, string $requested_redirect_to The requested redirect destination URL passed as parameter, WP_User $user WP_User object for the user logging out.

    Using the third param we can target specific redirect locations depending on the user, user role etc.

    function se_logout_redirect( $redirect_to, $requested_redirect_to, $user ) {
    	if ( in_array( 'subscriber', $user->roles ) ) {
    		$requested_redirect_to = home_url();
    	} else {
    		$requested_redirect_to = home_url( '/hello-world' );
    	}
     
    	return $requested_redirect_to;
     
    }
    add_filter( 'logout_redirect', 'se_logout_redirect', 10, 3 );
    
    Log in to add feedback

You must log in before being able to contribute a note or feedback.

  • About
  • News
  • Hosting
  • Donate
  • Support
  • Developers
  • Get Involved
  • Learn
  • Showcase
  • Plugins
  • Themes
  • Patterns
  • WordCamp
  • WordPress.TV
  • BuddyPress
  • bbPress
  • WordPress.com
  • Matt
  • Privacy
  • Public Code
WordPress.org
WordPress.org
  • Visit our Facebook page
  • Visit our Twitter account
Code is Poetry

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.