Leave a Reply

3 comments

  1. You can use the wp_logout_url() template tag to generate a logout link. You can specify the URL that the user will be redirected to as the first pramiter:

    wp_logout_url( 'http://example.com' );
    

    The above code will generate a URL that looks a bit like this: /wp-login.php?action=logout&_wpnonce=ba51bbcdc3&redirect_to=http://example.com/unsubscribe. You can make it into a link like so:

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

    If you want to use this in a page or post, you need to create a shortcode:

    function wpse_77939_unsubscribe_link() {
        return sprintf(
            '<a href="%s" title="Logout">Logout & Unsubscribe</a>',
            wp_logout_url( home_url( 'unsubscribe' ) )
        );
    add_shortcode( 'unsubscribe-link', 'wpse_77939_unsubscribe_link' );
    

    You can then add [unsubscribe-link] to a post or page where you want the link to appear.


    If you’re redirecting to another website besides your own, you need to add this code to your functions.php file (replace example.com with the domain of the site):

    add_filter( 'allowed_redirect_hosts','wpse_77938_allowed_redirect_hosts' );
    function wpse_77938_allowed_redirect_hosts( $allowed ) {
        $allowed[] = 'example.com';
        return $allowed;
    }
    
  2. Use this code if you are using it at menu at menu manger

    http://www.example.com/wp-login.php?action=logout&redirect_to=http://www.example.com/
    
  3. WordPress includes a template tag which you can use: wp_logout_url(). It’s a few simple lines of code if you want to redirect users to another URL after they sign out.

    I wrote a tutorial late last year on how to use this template tag. In summary, you need to create a filter in functions.php and then create a Logout link in sidebar.php.

    If you’re redirecting to another website besides your own, you need to add this code to your functions.php file (replace example.com with the domain of the site):

    add_filter( 'allowed_redirect_hosts','wpse_77938_allowed_redirect_hosts' );
    function wpse_77938_allowed_redirect_hosts($link) {
        $link[] = 'example.com';
        return $link;
    }
    

    You then need to add this line to your sidebar.php file or anywhere you can execute PHP:

    <a href="<?php echo wp_logout_url( 'http://example.com' ); ?>" title="Logout">Logout</a>