0

I want to create a site, where a normal user only is able to access the frontend. So there is no admin bar and everytime, they try to visit the admin area, they are just redirected to the frontpage.

I tried to use something like:

function redirect_non_admin_user(){
    if ( is_user_logged_in() && !current_user_can( 'administrator' ) ) {
        wp_redirect( site_url() );
        exit;
    }
}
add_action( 'admin_init', 'redirect_non_admin_user' );

The issue with the code above is, that the user now can't even post a comment anymore (I guess since posting a comments makes a POST request to the admin area and this is then redirected to the frontend).

So what would be a good solution for this? Let the user comment, but not access the admin area. Thanks!

3 Answers 3

0

The following code works for me, give it a try:

function no_dashboard() {
    $user = wp_get_current_user();
    if (in_array('subscriber', (array)$user->roles)) {
        wp_redirect(site_url());
        exit;
    }
}
add_action('admin_init', 'no_dashboard');
4
  • Yea but with that the comment function (and anything that sends data to the backend) will unfortunately not work as well :/
    – Zoker
    Commented Aug 3, 2021 at 21:06
  • I used this code on a production site with a custom theme and a number of plugins installed, plus on a test WP 5.8 plain vanilla site (no plugins, 2021 theme) and both sites worked with allowing comments and redirecting as specified. Are you sure you removed any other conflicting code? What role are you trying to get this to work with?
    – ScottM
    Commented Aug 3, 2021 at 23:07
  • Hmm well I tried your code and had the same issue. To be honest I don't really know, were this is coming from, I just know, that if I remove the action, posting comments works fine. If I include it, it does not. But as you can see in my own answer, I was able to solve this issue by not redirecting, if a POST action is sent to the backend.
    – Zoker
    Commented Aug 4, 2021 at 21:15
  • Are you in a position to set up a test site based on WP 5.8? If so, I would suggest doing so, then add the code (either the one snippet I provide or the one from VKS - both worked on my end) and confirm it works as expected. Then, start with adding the theme you are using to the test site, then the plugins - one by one - to figure out which one may be causing a conflict and not getting you the desired behavior.
    – ScottM
    Commented Aug 5, 2021 at 13:59
0

Add this on function.php

function custom_blockusers_init() {
  if ( is_user_logged_in() && is_admin() && !current_user_can( 'administrator' ) ) {
    wp_redirect( home_url() );
    exit;
  }
}
add_action( 'init', 'custom_blockusers_init' ); // Hook into 'init'
1
  • Unfortunately exact same issue. Comments can't be posted by the user
    – Zoker
    Commented Aug 4, 2021 at 14:19
0

I did some more testing and not redirecting if there is a $_POST['action'] fixes the issue:

function redirect_non_admin_user(){
    $action = $_POST['action'];
    if ( is_user_logged_in() && !current_user_can( 'administrator' ) && empty($action) ) {
        wp_redirect( site_url() );
        exit;
    }
}
add_action( 'admin_init', 'redirect_non_admin_user' );

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.