Allow access to more than one page in PHP/Wordpress - php

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();
}
}
});

Related

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;
}
}

Redirect Logged Out Users Wordpress

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;
}
}

WordPress Hook For Custom Redirect Behavior

I've been banging my head against the wall for the last little bit and could really use some help. I'm rather new to PHP and I'm sure this isn't too complicated but I just can't seem to get the behavior I need so I'm reaching out.
Basically I have a client whose doing a limited launch of their service and would like to create a custom login page they can share with early adopters. So basically what I need is the following:
When a user navigates to the site I first need to check if it's the register URL (www.example.com/selected-customer/register/). If this is true I want to redirect to the register page.
Next I want to check if this URL is any URL other than my default 'Coming Soon' page. If it is not the coming soon page I then need to check if the user is logged in. If they are not logged in I want to redirect them to the 'Coming Soon' page if not I want to render the page requested. I've tried to code a sample but it's not doing what I need it too.
Any help would be greatly appreciated :)
Sample Code:
add_action( 'template_redirect', function() {
if( ( !is_page('coming-soon') ) ) {
if( ( is_page('register') ) ) {
wp_redirect( home_url( '/captains-crew/register/' ) ); // redirect all...
exit();
}
if (!is_user_logged_in() ) {
wp_redirect( home_url( '/coming-soon/' ) ); // redirect all...
exit();
}
}
});
I didn't try yet but would work.
add_action( 'template_redirect', function() {
if(is_page('register'))
{
wp_redirect( home_url( '/captains-crew/register/' ) );
exit;
} elseif (!is_page('coming-soon') && !is_user_logged_in() ) {
wp_redirect( home_url( '/coming-soon/' ) );
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>";
}

Redirect to specific page if not logged in WordPress

I am trying to have it on my WordPress site to redirect to a landing page if the user is not logged in, but it always gets stuck in a redirect loop. This is basically what I have (in functions.php)...
add_action( 'template_redirect', function() {
if ( !is_user_logged_in() && ! is_page('login-to-view') ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
});
I know I am late for the party. I heard Big and Pac were rocking hard...
Anyways, this is what I usually do:
Create a login page, e.g. www.site.com/login
In my functions.php I add the following code:
add_action( 'template_redirect', function() {
if( ( !is_page('login') ) ) {
if (!is_user_logged_in() ) {
wp_redirect( site_url( '/login' ) ); // redirect all...
exit();
}
}
});
Explanation:
What happens above is access to every page on the site is restricted except for the login page. You can choose to exclude or include additional pages if you want and also the wp-admin URLs as well.
The issue is that I had the function in functions.php. is_page didn't know what page it is on yet. I moved it to the header, just before the doctype declaration and it works now with just this...
if ( ! is_page('login-to-view') && ! is_user_logged_in() ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
Another thing to note... I wasn't able to get it to work on my localhost, I'm thinking because site_url can't be found, and it was just redirecting to localhost:8888/login-to-view
I'm using MAMP, but I'm guessing you can set up URL paths like that in the PRO version.
Let's say the user is not logged in.
Then your code thinks in this way:
Is user logged in?
NO? Then do wp_redirect.
The user gets redirected to your page, and the process begins again. That's why you get the infinite loop.
Try altering the order of checks instead:
<?php
add_action( 'template_redirect', function() {
if ( ! is_page('login-to-view') && ! is_user_logged_in() ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
});

Categories