Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/WP/Viewable by Admin Only

August 8, 2016

Viewable by Admin Only

This article will show you how to make a WordPress Page viewable by the admin only. This is really quite handy if you’re creating a custom WordPress Template. You can make certain parts or the entire page viewable by the admin only by placing the code below in your WordPress Page Template.

if ( current_user_can( 'manage_options' ) ) {
    // display text here for user with admin privilege
} else {
    // display text here for user without admin privilege
}

if ( current_user_can( 'manage_options' ) ) { // display text here for user with admin privilege } else { // display text here for user without admin privilege }

There has been some confusion whether to use is_admin() instead of current_user_can(‘manage_options’). is_admin() is a conditional to check if the user is in the Dashboard or in the Administration Panel. It’s just a boolean conditional that will return true if the URL being accessed is in the admin section.

if ( is_admin() ) {
    // returns true that user is in the admin panel
} else {
    // returns false if otherwise
}

if ( is_admin() ) { // returns true that user is in the admin panel } else { // returns false if otherwise }

It’s best to use current_user_can(‘manage_options’) when checking if user is an admin.

Filed Under: WP Tagged With: admin

Content delivered to your email

About Me

I'm Ulysses, a Cloud Engineer at Cardinal Health based in Columbus, Ohio. I’m a certified AWS Solutions Architect. This website is my way of documenting the things I have learned in the Cloud. When off the grid, I enjoy riding my electric skateboard. I have surfed, snowboarded and played the saxophone in the past. I hope you will find this site helpful. It's powered by WordPress and hosted in AWS LightSail.

  • Cloud
  • Linux
  • Git

Copyright © 2012–2021