How to check if admin is the current user in WordPress

Checking the user's class is really important for the security of wordpress website. In most of cases, it is not required to check the user's class, because wordpress is a blog platform. However WordPress can be used for other kind of website so we need to know how to check the user's class.


In general, we can achieve this by checking what current user is capable of doing certain task. 'current_user_can()'

function checkAdmin(){
  if(current_user_can('administrator')) return true;
  return false;
}

Note: If we have a system/plugin that can create/modify/remove user's class(role) which is not really included in wordpress core, it is better use the function from that system/plugin. Because it is probably using certain name for the user classes(roles).

No comments:

Post a Comment