How to Display Content Based on User Capability in WordPress

Home » Snippets » How to Display Content Based on User Capability in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

As a website owner, you may want to control what content is displayed to certain users based on their capabilities or roles. Whether you want to show premium content to subscribers, restrict certain pages to administrators only, or create a membership site with different access levels, WordPress allows you to easily manage user capabilities and tailor content accordingly. In this article, we will explore different methods and plugins that you can use to display content based on user capabilities in WordPress.

					function wpturbo_display_content_based_on_capability() {
  if ( current_user_can( 'edit_posts' ) ) {
    echo '<p>' . __( 'Welcome, editor!', 'wpturbo' ) . '</p>';
  } elseif ( current_user_can( 'publish_posts' ) ) {
    echo '<p>' . __( 'Welcome, author!', 'wpturbo' ) . '</p>';
  } else {
    echo '<p>' . __( 'Welcome, user!', 'wpturbo' ) . '</p>';
  }
}
add_action( 'wp_footer', 'wpturbo_display_content_based_on_capability' );
				

In this section, we will explore how to display content based on a user’s capability in WordPress. This can be useful when you want to show different content or messages to users with specific roles or permissions.

The code snippet starts by defining a new function called wpturbo_display_content_based_on_capability(). Inside this function, we use conditional statements to check the current user’s capability.

The first condition checks if the current user has the capability to edit posts using the current_user_can() function. If the condition is true, we use the echo statement to display a welcome message tailored for editors. The message is wrapped in a <p> tag and is retrieved using the __() function for localization purposes.

if ( current_user_can( 'edit_posts' ) ) {
  echo '<p>' . __( 'Welcome, editor!', 'wpturbo' ) . '</p>';
} 

If the user does not have the capability to edit posts, we move to the next condition. The elseif statement checks if the current user can publish posts. If true, the code inside the block will execute, displaying a welcome message for authors.

elseif ( current_user_can( 'publish_posts' ) ) {
  echo '<p>' . __( 'Welcome, author!', 'wpturbo' ) . '</p>';
}

Finally, if none of the conditions above are met, the else block will be executed, displaying a generic welcome message for users.

else {
  echo '<p>' . __( 'Welcome, user!', 'wpturbo' ) . '</p>';
}

To ensure that the function is triggered and the content is displayed where desired, we use the add_action() function to hook the wpturbo_display_content_based_on_capability() function to the wp_footer action. This action fires just before the closing tag in the website’s HTML structure.

add_action( 'wp_footer', 'wpturbo_display_content_based_on_capability' );

By using this code snippet, you can create dynamic content based on the user’s capabilities, providing a personalized experience for different user roles.

Register an account to save your snippets or go Pro to get more features.