Redirect Logged Out Users Wordpress - php

I have a WordPress site where users go to the WooCommerce login/signup page before using the site. I want to add something to my functions.php that checks if the user is signed in and if they are on the login page, and if they are not it redirects them to the login page. The only code I can find redirects logged out users, so I was hoping someone could help.

Please add following code in your functions.php
add_action( 'template_redirect', 'redirect_to_login' );
function redirect_to_login() {
if ( is_page('my-account') && ! is_user_logged_in() ) {
wp_redirect( '/login/', 301 );
exit;
}
elseif ( is_page('login') && is_user_logged_in() ) {
wp_redirect( '/my-account/', 301 );
exit;
}
}

Related

WordPress restricted page redirect for non logged users to login page with PHP

I would like to redirect a visitor of a WordPress website to a login page when they are not logged in and on a certain page. I have the following PHP, which doesn't seem to work. Does anyone know what I am doing wrong?
add_action( 'admin_init', 'my_redirect_if_user_not_logged_in' );
function my_redirect_if_user_not_logged_in() {
if ( ! is_user_logged_in() && is_page( '3220' ) ) {
wp_redirect( 'https://www.loginpage.com ' );
exit;
}
}
template_redirect is the right hook to handle redirection.
Here is updated code:
add_action( 'template_redirect', 'my_redirect_if_user_not_logged_in' );
function my_redirect_if_user_not_logged_in() {
if ( ! is_user_logged_in() && is_page( 3220 ) ) {
wp_redirect( 'https://www.loginpage.com ' );
exit;
}
}

Best way to redirect user to login URL in WordPress?

I have numerous pages that require my users to be logged in when accessing them.
I've remapped my login page to mysite.com/login. So, currently for any page that requires user to be logged in, I would redirect them using the below.
//If user is not logged in...
if ( !is_user_logged_in() ) {
wp_redirect( wp_login_url() );
}
My question is, is the above good enough to ensure non-logged in users will always be redirected and not causing "too many redirect" errors.
Here are 2 examples please check you can add on function.php file
add_action( 'admin_init', 'redirect_non_logged_users_to_specific_page' );
function redirect_non_logged_users_to_specific_page() {
if ( !is_user_logged_in() && is_page('add page slug or ID here') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
wp_redirect( 'http://www.example.dev/your-page/' );
exit;
}
}
Put this in functions.php file and change the page ID or slug and the redirect url.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page('slug') && ! is_user_logged_in() ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}

Auto redirect a user who isn't logged into the Wordpress site

Does anyone know how to set functions.php when redirection to login page when a user isn't logged in?
Here's my code which creates some disabilities like no preview on article page etc...
add_filter( 'pre_get_posts', 'swpm_auto_redirect_non_members' );
function swpm_auto_redirect_non_members() {
if( !is_admin() && !SwpmMemberUtils::is_member_logged_in() && !is_page( array( 'membership-login', 'membership-join' )) ) {
wp_redirect( 'http://example.com/membership-login/' );
exit;
}
}
It should work only when website is in service not admin mode.
Thanks in advance.
try to make $_SESSION["member],
and make this
if(!isset($_SESSION["member"]))
{
echo "<script>location='/login.php'</script>";
}

Allow access to more than one page in PHP/Wordpress

I want to allow access to two areas. Signup and Signin within Woocommerce. If there user is not logged in they are redirected to the accounts page to select to either signup or login. But i want the signup on a seperate page or link within myaccount/signup
add_action( 'template_redirect', function() {
if( ( !is_page('accounts', 'signup') ) ) {
if (!is_user_logged_in() ) {
wp_redirect( site_url( '/accounts' ) ); // redirect all...
exit();
}
}
});
Is there something i am missing?
I am not sure if this the correct way to do this, but it worked for me. I am no expert so take this example with a grain of salt.
If will force a login if the user goes to any other page than signup or accounts/login.
// Redirect to accounts login page, if user is not logged in.
add_action( 'template_redirect', function() {
if( ( !is_page('accounts') && !is_page('signup') ) ) {
if (!is_user_logged_in() ) {
wp_redirect( site_url( '/accounts' ) ); // redirect all...
exit();
}
}
});

Redirect to front page wordpress if not logged in

I'm looking to redirect a user to a specific page if they are not logged in.
So blog.com/welcome
So they can either be viewing this page or wordpress login pages.
I was trying to modify this plugin
http://wordpress.org/plugins/redirect-to-login-if-not-logged-in/
add_action( 'wp', 'dmk_not_loggedin_redirect' );
function dmk_not_loggedin_redirect() {
global $pagenow;
However I am ending up with redirect loops. And I could use some help to stop this.
if ( ! is_user_logged_in() && $pagenow != 'wp-login.php' or 'http://www.blog.co.uk/front' )
wp_redirect( 'http://www.blog.co.uk/front' , 302 ); exit; }
I tried with the action hook init but the current post/page information is not available yet. But then, template_redirect does the job and it only acts on the front-end, login and admin pages work normally.
Here, there's a page with the slug welcome, to check for a post use is_single('post-slug'):
add_action( 'template_redirect', 'redirect_so_18688269' );
function redirect_so_18688269()
{
if( !is_user_logged_in() && !is_page( 'welcome' ) )
{
wp_redirect( site_url( 'welcome' ) );
exit();
}
}

Categories