0

I'm having some issues with my custom login page with redirections and restrictions. Any user can browse a 'community' page but they can't access the 'distributors' pages. I have set a rule so if anyone that it's not logged in will be forwarded to the home page. Those who are logged will be redirected to the 'distributor' pages. Everything seems to be working fine except on Firefox. For the custom login page I'm using a form from Elementor.

Here's custom code I have to restrict non-logged users to some pages.

/*
*   Restrict non logged users to certain pages
*/
add_action('template_redirect','my_non_logged_redirect', 10, 2);
function my_non_logged_redirect()
{
    if ((is_page('distributors') || is_page('distributor-success-stories') || is_page('distributor-solutions') || is_page('distributor-software') || is_page('distributor-product-software') || is_page('product-smart-projectors') || is_page('distributor-product') || is_page('product-accessories') || is_page('distributor-press') || is_page('distributor-media') || is_page('distributor-firmware')) && !is_user_logged_in())
    {
        wp_redirect( '/login' );
        die();
    }
}

Here's the code I have to redirect registered users after they successfully log in

/**
 * Redirect non-admins to the homepage after logging into the site.
 */
function custom_login_redirect( $redirect_to, $request, $user  ) {
    return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : '/distributors';
}
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );

Again, this is working fine on Chrome, Safari and Edge but it's not on Firefox. Can anyone help me on this one?

1 Answer 1

0

the redirect doesnt have a lot to do with browsers, have you try to clean firefox cache, it can be that.

also using '/distributors' on users after succefully log in as url can sometimes not work, can redirect instead for the url link plus '/distributors', is safer to use

home_url().'/distributors';

I use this function in functions.php to redirect on login

// redirects user after login
if (! function_exists('ctm_login')) {
    add_action('wp_login', 'ctm_login');
    function ctm_login(){wp_redirect(home_url());exit;}
}

so in your case should be something like

// redirects user after login
if (! function_exists('ctm_login')) {
    add_action('wp_login', 'ctm_login');
    function ctm_login(){
       $url = ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : home_url().'/distributors';
       wp_redirect($url);
       exit;
    }
}

this is the redirect after login, but one question, and if someone goes to the url and write "www.yourSite.com/distributors" will the page load?...

1
  • Thank you for your support @Tiago. If you check the code I posted above you'll see that there's a rule to block unlogged users to be redirected to the login page. In other words, if someone type /distributors or any of it's child pages, they will be redirected to the login page. The issue is that even after logged in, they can't access the distributors page and this happens in Firefox only. I already clear cache on all browsers, wordpress and the server.
    – Maluco
    Commented Oct 14, 2021 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.