WordPress how to check if a post is being viewed by admin

In WordPress you may want to do certain things only when admin user is viewing a post. Here is quick code snippet to know if an admin is viewing a post in wordpress:

if (current_user_can('edit_posts')) {
  // code which needs to be executed if admin is viewing the post

Here we are checking if a user has permission to edit the post. You can check some other wordpress capability also based on your requirements.

Note that this code should be used with caution as it can cause page to have different behaviour for admin users which may increase the testing effort on site.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments