Skip to content

Instantly share code, notes, and snippets.

@mgratch
Created May 8, 2015 22:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgratch/0778b9ef795ba1717875 to your computer and use it in GitHub Desktop.
Save mgratch/0778b9ef795ba1717875 to your computer and use it in GitHub Desktop.
Automatically redirect users to where they came from upon logging in and out. Warning this does cause a problem when logging out from a restricted content page.
<?php
function mg_rcp_hijack_login_url() {
global $rcp_options;
if( isset( $rcp_options['login_redirect'] ) ) {
$login_url = get_the_permalink( $rcp_options['login_redirect'] );
}
return $login_url;
}
function mg_login_logout( $args, $content, $tag ) {
if (is_user_logged_in()){
$href = wp_logout_url( get_the_permalink() );
}
else{
$redirect = get_the_permalink();
$login_url = mg_rcp_hijack_login_url();
if ( !empty($redirect) )
$login_url = add_query_arg('redirect_to', urlencode($redirect), $login_url);
$href = $login_url;
}
return $href;
}
add_shortcode( 'login-logout-button', 'mg_login_logout' );
function add_login_redirect_filter( $vars ){
$vars[] = "redirect_to";
return $vars;
}
add_filter( 'query_vars', 'add_login_redirect_filter' );
?>
<input type="hidden" name="rcp_redirect" value="<?php echo esc_url(get_query_var('redirect_to')); ?>" />
<input type="hidden" name="rcp_login_nonce" value="<?php echo wp_create_nonce( 'rcp-login-nonce' ); ?>"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment