WordPress.org

Ready to get started?Download WordPress

Forums

Restricting non admin to Dashboard (3 posts)

  1. Spacena
    Member
    Posted 1 year 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() );
    die();}
    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
    Member
    Posted 1 year 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
    Member
    Posted 1 year ago #

    Maybe have a look at how Peter does that:
    http://wordpress.org/extend/plugins/peters-login-redirect/

Topic Closed

This topic has been closed to new replies.

About this Topic