Redirect to Login
-
I am trying to protect a single page (with custom template – no content in actual page) with subscriber user access.
Is there an option to redirect non-logged in users to standard wp login form?
I am trying to redirect to other pages however all I get is an url with selected page and after ?redirect=[urlEncoded link to protected page], nothing happens after this.
-
same with me
I’m interested in this as well.
Thank you all for reporting this.
Do you mean that restricted content is still visible and that users are not being redirected?
Or that a redirect happens, just not to the page selected for that Acces Level?
To redirect to custom URLs, just pass in the full link in the input field instead of selecting from the dropdown.
To show a login form on any page, you can use the
[login-form]
shortcode.In my case I’m using the [login-form] shortcode. When the user logs in he is ‘redirected’ to the same page. Now the user is logged but goes back to the same page with the [login-form]. And in the address bar I have the selected page url + ?redirect=[urlEncoded link to protected page]
Found my problem… My theme (Boss Theme) has a redirect to previous page function (code below) that redirect the user to previous page. This function is very nice, because if I disable it the users are redirected to the dashboard after login.
Is there a way to keep both functions working together? When the login is on a restricted page use rua plugin redirect. When not use this theme function…
/* * **************************** LOGIN FUNCTIONS ***************************** */ function buddyboss_is_login_page() { return in_array( $GLOBALS[ 'pagenow' ], array( 'wp-login.php', 'wp-register.php' ) ); } add_filter( 'login_redirect', 'buddyboss_redirect_previous_page', 10, 3 ); function buddyboss_redirect_previous_page( $redirect_to, $request, $user ) { if ( buddyboss_is_bp_active() ) { $bp_pages = bp_get_option( 'bp-pages' ); $activate_page_id = !empty( $bp_pages ) && isset( $bp_pages[ 'activate' ] ) ? $bp_pages[ 'activate' ] : null; if ( (int) $activate_page_id <= 0 ) { return $redirect_to; } $activate_page = get_post( $activate_page_id ); if ( empty( $activate_page ) || empty( $activate_page->post_name ) ) { return $redirect_to; } $activate_page_slug = $activate_page->post_name; if ( strpos( $request, '/' . $activate_page_slug ) !== false ) { $redirect_to = home_url(); } } $request = isset( $_SERVER[ "HTTP_REFERER" ] ) && !empty( $_SERVER[ "HTTP_REFERER" ] ) ? $_SERVER[ "HTTP_REFERER" ] : false; if ( !$request ) { return $redirect_to; } $req_parts = explode( '/', $request ); $req_part = array_pop( $req_parts ); if ( substr( $req_part, 0, 3 ) == 'wp-' ) { return $redirect_to; } $request = str_replace( array( '?loggedout=true', '&loggedout=true' ), '', $request ); return $request; }
Thank you for providing the snippet, I will try to look into that.
Everyone:
Unfortunately I am not able to reproduce the problem with redirection.
If I restrict some pages and set the level to redirect to a Login page with the shortcode, unauthorized users will be redirected to that page. After login, they are successfully redirected back to the page they came from.
It is likely that there is a conflict with other plugins or themes as well, but it is also worth checking this:
1. If you have more than 1 Access Level, make sure that the page you try to redirect the user to is not restricted by another level.
2. Make sure that the login page is not restricted.Hi @intoxstudio
Thanks for your reply. I was able to do a workaround for my site using the code below in my child theme functions.php file.
It’s not a good solution because it is based on the slug of the page that contains the [login-form] shortcode. But was the only way I was able to fix it for my site.
//Removes the filter that was causing the conflict with Restrict User Access add_action( 'after_setup_theme', 'childtheme_remove_filters' ); function childtheme_remove_filters(){ remove_filter( 'login_redirect', 'buddyboss_redirect_previous_page', 10 , 3 ); } //Add new filter that checks if it's on the page where the [login-form] form shortcode is used. It's not a beatiful solution because it is based on the page slug. But was what I was able to do :) add_filter( 'login_redirect', 'child_buddyboss_redirect_previous_page', 100, 3 ); function child_buddyboss_redirect_previous_page( $redirect_to, $request, $user ) { $aviso_parts = explode( '/', $_SERVER[ "HTTP_REFERER" ]); $aviso_part = $aviso_parts[3]; if ( $aviso_part == 'aviso' ) { if(isset($_GET['redirect_to'])) { $redirect_to = urldecode($_GET['redirect_to']); return $redirect_to; } else { return $redirect_to; } } else { if ( buddyboss_is_bp_active() ) { $bp_pages = bp_get_option( 'bp-pages' ); $activate_page_id = !empty( $bp_pages ) && isset( $bp_pages[ 'activate' ] ) ? $bp_pages[ 'activate' ] : null; if ( (int) $activate_page_id <= 0 ) { return $redirect_to; } $activate_page = get_post( $activate_page_id ); if ( empty( $activate_page ) || empty( $activate_page->post_name ) ) { return $redirect_to; } $activate_page_slug = $activate_page->post_name; if ( strpos( $request, '/' . $activate_page_slug ) !== false ) { $redirect_to = home_url(); } } $request = isset( $_SERVER[ "HTTP_REFERER" ] ) && !empty( $_SERVER[ "HTTP_REFERER" ] ) ? $_SERVER[ "HTTP_REFERER" ] : false; if ( !$request ) { return $redirect_to; } $req_parts = explode( '/', $request ); $req_part = array_pop( $req_parts ); if ( substr( $req_part, 0, 3 ) == 'wp-' ) { return $redirect_to; } $request = str_replace( array( '?loggedout=true', '&loggedout=true' ), '', $request ); return $request; } }
- The topic ‘Redirect to Login’ is closed to new replies.