WP custom logout

I’m developing a custom logout page using templates (you know, creating simple pages and adding them “/* Template Name: MyTemplate */”). This way I can access all wp core features:

<?php /*Template Name: My Logout */
    wp_logout();
?>

The problem is when I call this template using Ajax I’m getting the same warning:

Read More

Warning: Cannot modify header information – headers already sent by (output started at /XXX/mylogout.php:1) in /XXX/wp-includes/pluggable.php on line 697

Yes it’s a warning but the logout action is not done (if I try to access member page, I can without log in).

I’m have been reading about blank spaces in functions.php and similars but looks like all it’s fine. Maybe “Template Name…” comment??

Any ideas??

Thanks in advance.

Related posts

Leave a Reply

2 comments

  1. You can create a logout link with:

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

    where it will redirect you to the home page. You can replace it with a custom redirect page.

    Check out the wp_logout_url() function in the Codex here.

  2. Another solution is to check logout page on the init hook to avoid headers already sent error…

    add_action('init', '_logout_func');
    
    function _logout_func() {
       if(stristr($_SERVER['REQUEST_URI'],'logout'))
           wp_logout();
    }