Strange issues when i try to use whitelisted domains and redirection

This topic has 7 replies, 3 voices, and was last updated 3 months, 2 weeks ago ago by Andrew Mitchell

  • Avatar: Aulakh, Sam
    Aulakh, Sam
    Participant
    June 19, 2024 at 00:08
    // 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.

    6 Answers
    Avatar: Justin
    Luca Rossi
    Support staff
    June 19, 2024 at 10:38

    Hi @Aulakh, Sam,

    The login form on My Account page is related to the WooCommerce plugin, so please also add this code:

    
    function redirect_after_login_woo($redirect_to, $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('woocommerce_login_redirect', 'redirect_after_login_woo', 10, 2);
    

    Please note that further cusomization codes will fall out of scope our support service.

    If you require personal customization services, we invite you to submit a request through our customization panel at: https://www.8theme.com/account/#etheme_customization_panel , and continue conversations with the technicians team directly.
    Please be advised that customization services will incur additional charges, the exact amount of which will be communicated following a detailed review of your request.

    Best Regards,
    [Luca Rossi]
    8Theme’s Team.

    Avatar: Aulakh, Sam
    Aulakh, Sam
    Participant
    June 19, 2024 at 14:02

    Deeply appreciate your guidance in right direction thanks luca

    Avatar: Aulakh, Sam
    Aulakh, Sam
    Participant
    June 19, 2024 at 14:39
    // 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);
    
    function redirect_after_login_woo($redirect_to, $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('woocommerce_login_redirect', 'redirect_after_login_woo', 10, 2);

    so will this be the full code when you say add to the code

    Avatar: Aulakh, Sam
    Aulakh, Sam
    Participant
    June 19, 2024 at 14:51

    .

    Avatar: Aulakh, Sam
    Aulakh, Sam
    Participant
    June 19, 2024 at 14:55

    But why does register option of yours o n my account doesnt follow what i wrote for whitelist i check it works and check domain whitelisting in wp-login but not in ur register area

    Avatar: Andrew Mitchell
    Andrew Mitchell
    Support staff
    June 19, 2024 at 16:36

    Dear Aulakh, Sam,

    As we continue our mission to exceed expectations, your insights become increasingly valuable. Could we, with all due respect, request your thoughtful feedback by giving our theme a deserved 5-star rating on ThemeForest?

    Click here to share your valuable perspective: https://themeforest.net/downloads

    Your time and trust are highly appreciated!

    Best Regards,
    The 8Theme Team

  • Viewing 7 results - 1 through 7 (of 7 total)

The issue related to '‘Strange issues when i try to use whitelisted domains and redirection’' has been successfully resolved, and the topic is now closed for further responses

8theme customization service
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.