1

I have written this function for loggin user out. The user is loggin out but not redirecting to the page instead goes to home page which default logout Url. i have tried wp_logout_url() and also wp_redirect().

function wc_registration_redirect( $redirect_to) {
    wp_logout();
    wp_redirect( '/my-account');
    exit;
}
2

The correct method for changing the logout redirect is the logout_redirect filter:

/**
 * Filters the log out redirect URL.
 *
 * @since 4.2.0
 *
 * @param string  $redirect_to           The redirect destination URL.
 * @param string  $requested_redirect_to The requested redirect destination URL passed as a parameter.
 * @param WP_User $user                  The WP_User object for the user that's logging out.
 */
add_filter( 'logout_redirect', function( $redirect_to, $requested_redirect_to, $user ) {
    if ( ! $requested_redirect_to ) { // Don't override the redirect if one was already set in the logout URL
        $redirect = home_url( user_trailingslashit( 'my-account' ) );
    }

    return $redirect;
}, 10, 3 );
1

You need to hook it to wp_logout actions hook and remove the wp_logout(); from the function. It'll look like below-

add_action('wp_logout', 'wc_registration_redirect');

function wc_registration_redirect( $redirect_to) {
    wp_redirect( '/my-account');
    exit;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.