You could think “I just need to install a plugin, there are at least a dozen of plugins that do this”. That’s true, there are a lot of plugins that limit the access to the dashboard of WordPress, but are you sure that a plugin is always the best choose?

If you want limit the access to your WordPress dashboard avoiding side effects, or if you tried to do it programmatically by yourself and something gone wrong, this is the article which you are looking for.

Why avoid to use a plugin

There are a lot of plugins that can help you to achieve this result, just few example:

So why should you hide your WordPress dashboard programmatically, instead of use one of these plugins? Because an elevated number of plugins can cause many side effects. Foremost it can increase the loading time of your site, making it slower and unpleasant for your users. In addition you have to update periodically one more plugin and, if the plugin is not longer updated and supported by its author, it could become a security risk for all your site.

In my opinion, there is really no reason to take these risks, because you can achieve the same result just writing down very few lines of code.

Hide the WordPress dashboard avoiding the AJAX trap

As I told, it’s possible to limit access to the dashboard with few lines of code, it’s actually just a redirect with an if statement, so why many customers have failed and asked us support for this easy task? Because they have not considered the AJAX trap.

So let me show you the wrong way to achieve it. Usually the code of our customers was something like that:

What is wrong in this code? Actually there is nothing really wrong, but there is something missing. Using this code, you forget that every ajax call handled by WordPress is sent to which is an admin page. This mean that the code below blocks every ajax call of your site.

In the light of this new consideration, this is the working code:

NOTE: You may read around that current_user_can( 'manage_users' ) and current_user_can( 'administrator' ) return always the same result. This is wrong because current_user_can( 'administrator' ) creates troubles in multisite installations, because it return false for superadmins.

Limit access to the admin panel only to certain roles

If you want to limit the access to many specific roles and you want avoid to add such many OR statement, you can do in this way:


You have discovered that rely you to third-part plugins is not always a good idea, it can have some side effects and sometime,like in this case, the result is really really easy to achieve the same result programmatically.

If you have any question or you think that that something in the code should be even improved, or if you liked the article and you want just say thank you, it would be really appreciated to see a comment right below.


Written by


<p>WordPress lover since v3.5. Back end and front end developer; enthusiastic themes and plugins creator; also getting along with Laravel and Vue JS. Founder of Webbaku and member of Alkaweb team since 2015.</p>

1 Comment

  1. HI Antonio,

    Thanks for the awesome sharing for the code.
    “Limit access to the admin panel only to certain roles ”
    I try the code as above on my website and it works perfectly !
    However I found a loophole which i need your advise.
    The restriction works on Desktop.
    I tried to login via my mobile( as a vendor role ), then go to ; i can view the Dashboard with all ADMIN functions ! ( Does Not Work on Mobile )

    I am using woocommerce, and there’s couple of roles.
    I would like to restrict dashboard for all roles except admin and editor

    Looking forward for your advise.
    Thanks for the awesome work !

Leave a Reply

Your email address will not be published.Required

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

You May Also Like to Read