// Redirect Users to My Account Page if Not Logged In
function restrict_access_to_logged_in_users() {
if (!is_user_logged_in() && !is_page('my-account') && !is_page('register')) {
wp_redirect(home_url('/my-account/'));
exit;
}
}
add_action('template_redirect', 'restrict_access_to_logged_in_users');
// Redirect users to /catalogue/ after login
function redirect_after_login($redirect_to, $request, $user) {
// Check if there is a valid user object
if (isset($user->roles) && is_array($user->roles)) {
// Check if the user has any role, assuming catalogue is accessible to all logged in users
return home_url('/catalogue/');
} else {
// Return the default behavior
return $redirect_to;
}
}
add_filter('login_redirect', 'redirect_after_login', 10, 3);
this above code i want for redirection
this below code is my whitelist for domain
<?php
/*
Plugin Name: Domain Whitelist for Registration
Description: Allow only users with whitelisted domains to register. Assigns them the role of customer.
Version: 1.1
Author: Galactic Design Labs Pte. Ltd.
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
// Hook to add custom registration validation
add_action('register_post', 'check_whitelisted_domain', 10, 3);
function check_whitelisted_domain($sanitized_user_login, $user_email, $errors) {
$whitelisted_domains = get_option('whitelisted_domains');
if (!empty($whitelisted_domains)) {
$whitelisted_domains = explode(',', $whitelisted_domains);
$email_domain = substr(strrchr($user_email, "@"), 1);
if (!in_array($email_domain, $whitelisted_domains)) {
$errors->add('domain_not_whitelisted', __('Sorry, your email domain is not allowed for registration.'));
}
}
}
// Hook to assign customer role to newly registered users
add_action('user_register', 'assign_customer_role_to_whitelisted_users');
function assign_customer_role_to_whitelisted_users($user_id) {
$user = get_user_by('ID', $user_id);
$whitelisted_domains = get_option('whitelisted_domains');
if (!empty($whitelisted_domains)) {
$whitelisted_domains = explode(',', $whitelisted_domains);
$email_domain = substr(strrchr($user->user_email, "@"), 1);
if (in_array($email_domain, $whitelisted_domains)) {
$user->set_role('customer');
}
}
}
// Admin settings for whitelisted domains
add_action('admin_menu', 'whitelist_domain_menu');
function whitelist_domain_menu() {
add_menu_page('Domain Whitelist Settings', 'Whitelist Domains', 'manage_options', 'whitelist-domain-settings', 'whitelist_domain_settings_page');
add_action('admin_init', 'register_whitelist_domain_settings');
}
function register_whitelist_domain_settings() {
register_setting('whitelist-domain-settings-group', 'whitelisted_domains');
}
function whitelist_domain_settings_page() {
?>
<div class="wrap">
<h1>Whitelist Domains</h1>
<form method="post" action="options.php">
<?php settings_fields('whitelist-domain-settings-group'); ?>
<?php do_settings_sections('whitelist-domain-settings-group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Whitelisted Domains</th>
<td><input type="text" name="whitelisted_domains" value="<?php echo esc_attr(get_option('whitelisted_domains')); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Enqueue necessary scripts and styles for the admin page
add_action('admin_enqueue_scripts', 'enqueue_whitelist_admin_styles');
function enqueue_whitelist_admin_styles($hook) {
if ($hook != 'toplevel_page_whitelist-domain-settings') {
return;
}
wp_enqueue_style('whitelist_admin_styles', plugins_url('/css/admin-style.css', __FILE__));
}
// Enqueue necessary scripts and styles for the registration page
add_action('wp_enqueue_scripts', 'enqueue_whitelist_frontend_styles');
function enqueue_whitelist_frontend_styles() {
if (is_page('register') || is_page('wp-login.php?action=register')) {
wp_enqueue_style('whitelist_frontend_styles', plugins_url('/css/frontend-style.css', __FILE__));
}
}
?>
=======================
Problem is when i use /my-account/ to login the redirect to /catalogue/ doesnt work
so user goes to site he is redirected to my account page for sign in and register but the following happens when i try to register using /my-acoount/ register it pickups automatically and doesnt validate for whitelisted domains.
also when i login using the my account page it redirects to my-account not /catalogue/
i want to use this instead wp-login for secuirty i tried all this in stagging site offline.