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 Answers
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="https://wordpress.stackexchange.com/questions/77937/<?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="https://wordpress.stackexchange.com/questions/77937/%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;
}