2

I am trying to create an unsubscribe page for my members, but in order to see the unsubscribe form, they have to be logged out, so the first link is:

/wp-login.php?action=logout&_wpnonce=ba51bbcdc3. 

Once they are logged out, I need them redirected to the unsubscribe form, but I can't just set up a forward, because this is also the login page.

3

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;
}
  • This answer expands on @WordPress Kid's post – shea Jan 2 '13 at 6:31
1

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/
0

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>
  • 1
    Please explain how to do it as part of your answer – shea Jan 2 '13 at 5:55
  • Done @bungeshea. – Wordpress Kid Jan 2 '13 at 6:02

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.