Tuesday, 16 October 2018

How to Display Content Based on WordPress User Roles


When building a WordPress website, it’s often useful to provide content or functionality based on a user’s role or capabilities. For example, you may want to display some special content on your site – but only for administrators. That’s just one of many possibilities. It’s quite handy that WordPress has a built-in function to help. The current_user_can() function allows you to check the permissions of logged-in users. Based on that information, you can provide them with whatever special goodies you like. Conversely, you can also turn off certain items as well.
Keeping with the previously mentioned special content example, we’ll dive into a few basic snippets that let us add this functionality.
Example 1: Administrators Only
In this example, we’ll check to see if the logged in user visiting our page is a site administrator. If they are, a little welcome message will be displayed.
Before we go into the code, it’s worth noting that there is more than one way to check a user’s permissions. The WordPress Codex states that we can provide an existing user role inside the current_user_can() function, however, it’s not recommended.
Source: https://managewp.org/articles/17980/how-to-display-content-based-on-wordpress-user-roles



source https://williechiu40.wordpress.com/2018/10/16/how-to-display-content-based-on-wordpress-user-roles/

No comments:

Post a Comment