• Hi there

    My client is using WordPress as an Intranet on a closed network (no internet access). When a staff member first visits the site, our PKI script adds them automatically as a user and assigns them a role based on their department.

    Occasionally the format of the PKI certificate isn’t consistent with what our script is looking for and the user is added to the WordPress ‘New User Default Role’ as defined in the settings. We’ve called this role ‘PKI Error’. This role only has one capability, which we’ve called ‘pki_error_redirect’. We check this group once or twice a week and manually update each user, assigning them to the correct role.

    In the meantime, these users are able to view the intranet without too many issues – but I’d like to make the site inaccessible to our default role, redirecting them to a help page (a static page hosted elsewhere) or message.

    I had thought that adding a redirect based on current_user_can would be the answer, but I’ve been unsuccessful so far! I’ve tried adding it to the theme’s functions.php and as a custom plugin.

    I’ve tried:

    function redirect_by_role () {
    global $current_user, $wp_roles;
    	if( current_user_can ( 'pki_error_redirect' ) ) {
    		return 'https://the-help-page-address';
    	} else {
    		return 'https://the-intranet-address';
    	}
    }
    add_filter('login_redirect', 'plugin_admin_redirect');

    and

    function wps_login_redirect_contributors() {
      if ( current_user_can('pki_error_redirect') ){
          return 'https://the-intranet-address';
      }
    }
    
    add_filter('login_redirect', 'wps_login_redirect_contributors');

    And a few more!

    I’m slowly pulling my hair out, it’d be so great if somebody could help.

    Many thanks,
    Karen

Viewing 4 replies - 1 through 4 (of 4 total)
  • In order for those advanced enough here to help with both an intranet issue and the specific ‘PKI script’ noted (nor any other functions that may be in use), we would need access to them which we do not.

    That said,

    current_user_can

    may not be your best choice of methods.

    See:

    https://codex.www.ads-software.com/Roles_and_Capabilities#Other_Codex_Articles

    Thread Starter DesignseedNZ

    (@designseednz)

    I’ve just realised that the redirect snippets I was trying to use redirect after login. Seeing as the PKI script logs everyone in automatically and presents the homepage, I guess I just need a simple redirect based on capability, or a message denying access to the site if you’re in a certain role. Is that something you can help with?

    Is that something you can help with?

    Those who help here wish to continue to for the sake of the WordPress Community, but are not allowed to solicit work in this forum or otherwise discuss such directly here.

    Thread Starter DesignseedNZ

    (@designseednz)

    What? I just meant help explain or help come up with an idea for me to try. I’m not soliciting anything.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Redirect Using current_user_can’ is closed to new replies.