In this code, replace 2024 with the ID if the new-login page. To get the page ID, edit the page in the backend - the number in the URL such as (?post=2024) is the ID.
The redirect to the new login page worked. However, if users try to access the shop page and my account page directly from the URL, it works. They are only redirected to the new login page if they click some category or any other page. You can't see any products but you can see the website.
How can I solve this? Maybe add some code snippet that detects if a guest (or a user that is not logged in) is trying to access the website and redirect them to the login page?
I tried to implement this custom code snippet to check if the user is not logged in and is visiting pages other than "login" or "lost-password" in order to redirect them back to the login page.
$login_page = '/login';
$lost_password_page = '/lost-password';
// Check if user is logged in
$is_logged_in = is_user_logged_in();
// Get the relative path of the current page
$current_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Check if user is visiting the login page or the lost password page.
$is_login_page = (strpos($current_path, trim($login_page, '/')) !== false);
$is_lost_password_page = (strpos($current_path, trim($lost_password_page, '/')) !== false);
// If we're not on the login/lost-password pages, it should redirect
if (!$is_logged_in && !$is_login_page && !$is_lost_password_page) {
echo "<script>alert('You're a guest. You're being redirected.');</script>";
wp_redirect(home_url($login_page)); // Redireciona para a página de login
exit; // Garante que o script será interrompido após o redirecionamento
} else {
echo "<script>alert('You are logged in or you are on the login or lost password page');</script>";
}
However, this code does not work.. I tried doing this way because I don't know the B2BKing guest shop hooks.. Maybe some B2BKing code is having a higher priority.. How do we check if the user is a guest and is visiting those pages?
You can additionally also redirect the Shop and Account pages to your custom page, by also adding this snippet:
add_action('template_redirect', function(){
if (!is_user_logged_in()){
if (is_account_page() || is_shop()){
wp_redirect(get_permalink(2024)); // replace 2024 with your page ID
exit();
}
}
}, 10000);
Replace 2024 with the page ID of your redirect page.
Hi!
I'm bulding a custom login page for the store, since it has restricted access.
How can I change the login URL?
The option "Hide website / force login" option is enabled, so every user is sent to the login page before entering the store
My custom login page is something like "https://miguelz56.sg-host.com/new-login/". Is it possible to send the users to this page instead of "https://miguelz56.sg-host.com/wp-login.php" ?
Best regards and thanks
Hello Miguel,
To achieve that, I don't think we can use 'hide website / force login', but I think we could use the "hide shop & products" option as follows:
(1) In B2BKing -> Settings -> Access choose Hide shop & products + Restrict all pages: https://prnt.sc/svrzxVYlH_R2
(2) Add this PHP code snippet to your site:
add_filter('b2bking_guest_shop_redirect_conditional', function($redirect){ if (is_page(2024)){ return false; } return $redirect; }, 10, 1); add_filter('b2bking_guest_shop_redirect_link', function($link){ return get_permalink(2024); }, 10, 1);In this code, replace 2024 with the ID if the new-login page. To get the page ID, edit the page in the backend - the number in the URL such as (?post=2024) is the ID.
Let me know if that works for you,
Kind regards,
Stefan
Hi!
The redirect to the new login page worked. However, if users try to access the shop page and my account page directly from the URL, it works. They are only redirected to the new login page if they click some category or any other page. You can't see any products but you can see the website.
You can try it from this pages:
Shop page: https://miguelz56.sg-host.com/loja-produtos/
Account page: https://miguelz56.sg-host.com/minha-conta/
How can I solve this? Maybe add some code snippet that detects if a guest (or a user that is not logged in) is trying to access the website and redirect them to the login page?
Best regards and thanks
Hi Stefan,
I tried to implement this custom code snippet to check if the user is not logged in and is visiting pages other than "login" or "lost-password" in order to redirect them back to the login page.
$login_page = '/login'; $lost_password_page = '/lost-password'; // Check if user is logged in $is_logged_in = is_user_logged_in(); // Get the relative path of the current page $current_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // Check if user is visiting the login page or the lost password page. $is_login_page = (strpos($current_path, trim($login_page, '/')) !== false); $is_lost_password_page = (strpos($current_path, trim($lost_password_page, '/')) !== false); // If we're not on the login/lost-password pages, it should redirect if (!$is_logged_in && !$is_login_page && !$is_lost_password_page) { echo "<script>alert('You're a guest. You're being redirected.');</script>"; wp_redirect(home_url($login_page)); // Redireciona para a página de login exit; // Garante que o script será interrompido após o redirecionamento } else { echo "<script>alert('You are logged in or you are on the login or lost password page');</script>"; }However, this code does not work.. I tried doing this way because I don't know the B2BKing guest shop hooks..
Maybe some B2BKing code is having a higher priority..
How do we check if the user is a guest and is visiting those pages?
Best regards and thanks!
Hi again,
You can additionally also redirect the Shop and Account pages to your custom page, by also adding this snippet:
add_action('template_redirect', function(){ if (!is_user_logged_in()){ if (is_account_page() || is_shop()){ wp_redirect(get_permalink(2024)); // replace 2024 with your page ID exit(); } } }, 10000);Replace 2024 with the page ID of your redirect page.
Let me know if you run into any issues,
Hi Stefan,
Thanks! You're the man. It's working perfectly.
Best regards and Merry Christmas