Brian Schroeter Brian Schroeter - 6 months ago 16
PHP Question

Wordpress - Logout redirection based on role

I'm trying to determine how I can redirect users upon logout, to a URL defined by their role. Simply put, I want to redirect admins (as well as editors) that logout to a different URL, than subscribers / privileged users.

I'm using the following code to redirect users at logout right now, but this redirects everyone. Any insight as to how I can have a different redirect based on their account role, would be great!

/**
* Redirect to custom login page after the user has been logged out.
*/
public function redirect_after_logout() {
$redirect_url = home_url( 'member-login?logged_out=true' );
wp_safe_redirect( $redirect_url );
exit;
}

add_action( 'wp_logout', array( $this, 'redirect_after_logout' ) );


Thanks!

Answer

Im not sure didnt tested but you can use user roles with if else statement

    function redirect_after_logout() {
         if (!current_user_can('manage_options')) { $url = '/';
         } else { $url = 'member-login?logged_out=true'; }
        $redirect_url = home_url( $url );
        wp_safe_redirect( $redirect_url );
        exit;
    }
add_action( 'wp_logout', array( $this, 'redirect_after_logout' ) );