Restricting non admin to Dashboard (3 posts)

  1. Spacena
    Posted 3 years ago #

    There is a situation here. I've tried the following code to prevent the non admins to access the dashboard.

    add_action('admin_init', 'no_mo_dashboard');
    function no_mo_dashboard() {
      if (!current_user_can('manage_options') && $_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {
      wp_redirect(home_url()); exit;

    and this as well..

    function baw_no_admin_access()
    if( !current_user_can( 'administrator' ) )
    wp_redirect( home_url() );
    add_action( 'admin_init', 'baw_no_admin_access', 1 );

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    Both codes are doing what they are supposed to. But one unusual behavior I noticed that the non admins are not able to post anything. Neither any posts nor any comments of non admins are posted. What's wrong ?

  2. Spacena
    Posted 3 years ago #

    If I remove the wp_redirect(home_url()) part, the code doesn't bring any unexpected result. Though the users are prevented to access the restricted area, they are sent to /wp-admin page with nothing displayed as they are no redirects now.

  3. leejosepho
    Posted 3 years ago #

    Maybe have a look at how Peter does that:

Topic Closed

This topic has been closed to new replies.

About this Topic