0

I have added custom page '/member-logout/' in nav walker for user logout. When activated then the function 'render-logout' is fired

function render_logout() {
    if ( is_user_logged_in() ){
         wp_logout();
    } else {
        echo "You are not logged-in"; 
    }
}

the logout works but generates a lot of warnigns of

Warning: Cannot modify header information - headers already sent by (output started at /home/........./formatting.php:5081) in /home/s..../wp-includes/pluggable.php on line 915

and i cannot find what is at fault

but redirect after logout does not work

    public function redirect_after_logout() {
    $redirect_url = home_url( 'member-login' );
    wp_redirect( $redirect_url );
    exit;
}
add_action( 'wp_logout', 'redirect_after_logout' );

The logout link below works very well (no warnings) but this is not loaded automaticly.

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

Any idea how to run this link using wp_redirect or something similar?

0

You could create a custom Page template which contains only the logout code, no call to header.php etc. When you load the /member-logout/ page you're probably calling the default page.php template which is loading header.php etc. If instead you apply your custom Page template it will just perform the logout action and can then redirect, as no headers have been sent (headers being different than just header.php but as soon as you output any HTML, which is contained in header.php, you have headers which is preventing the redirect).

  • Thank you for info. Quite strange issue with this wp-logout(). I have tested it on three different wordpress instalations (2 localhosts and one online-service) and it works on one local but doesnt on the rest. Do you think that any other solution exists? or maybe how to add the link only to the nav walker submenu? – Greg Skala Mar 21 '17 at 19:46
  • I do see wp_logout_url can take a redirect parameter so maybe if you combined it in one call it would make a difference. – WebElaine Mar 21 '17 at 21:42
  • any suggestion how to fire "wp_logout_url( home_url() )" when the page is loading? or maybe using wp_redirect and wp_logout_url? – Greg Skala Mar 22 '17 at 8:29
  • i have tried "wp_redirect( wp_logout_url() );" but logout does not work and still the same warning – Greg Skala Mar 22 '17 at 9:27

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.