Over a million developers have joined DZone.

[WordPress] How to check if current user is Administrator

· Web Dev Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

It’s been quite a while since I posted anything on this blog. Have been really busy doing development work for others and could not devote time to blogging.

I have a quick snippet to share, which is most commonly used while programming in WordPress. The following code snippet let’s you check if the current user logged in is an Administrator or not.

function dm_is_admin(){
    return current_user_can( 'manage_options' );
}

Paste the above code inside functions.phpof the active theme. Please note that I have used the function name as dm_is_admin, since is_admin function for WordPress already exists. However, the built-in is_admin function only checks if you are currently on the WordPress Dashboard or no.

You can use this code snippet to display certain information in the front end only when the Admin is logged in. I use this to display debugging information on any live site, this comes very handy.

Hope this is going to help you, as well!
Stay Digified!!
Sachin Khosla

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Sachin Khosla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}