Enable private content to specific roles in WordPress – .com

In this tutorial we will see how to restrict and enable private content in WordPress for a specific user role.

Private content in WordPress

By default WordPress offers a couple of alternatives to create private content through the visibility of posts and pages.

  • On the one hand, we can protect our content with a password and in this way only users who have the password will be able to access it.
  • On the other hand we have the possibility to mark our pages and entries as “Private”. When this happens only users with sufficient permissions can read those articles or pages.

But what if we want to make our WordPress totally private? What if we want to enable access to certain private content only to specific roles? We will not find either of these two options in the WordPress menus by default. However, we can easily add these features, like many others, through plugins and small code snippets.

This custom code can be included in a number of ways. If you need information to do it, you can access this tutorial in which we will learn how to . In this case we will choose to use a plugin that we have created specifically for these issues. In the next section we will see the steps to follow.

Extra content for specific users

At this point we are going to learn how to enable content restricted to certain groups of users. In this way we can offer special or extra content to part of the users of our website, which may interest us for various reasons.

To do so, we have the option of assigning reading passwords to the pages or entries that we want, as we discussed above.

By simply sharing our password with those users, they could enter the same and see the content of that article.

This is functional, and can be done easily. But if we want to create and maintain a small membership site, it may not be very efficient. If we change the password periodically or use different passwords in our content, this method will give us more work than necessary.

See also  Online Marketing Blog

A more practical way to enable access to private content would be to use a small code with which we grant special read permissions to the role or roles that interest us. In the next point we will see how to do it.

Enable access to private content based on user role

We can configure the visibility of our posts and pages in WordPress by marking our posts and pages with the “Private” option that we will find in the “Publish” menu when editing our content.

Private posts and pages are only accessible to users with Super Admin, Administrator and Editor roles. That is, if we mark the visibility of a post or page as “Private”, by default the subscribers, collaborators, and authors registered on our website will not be able to see it because they do not have sufficient permissions or capabilities.

So how do we that a private content is also accessible to our subscribers or to any other role? What we are going to do is add a little code with which we grant those reading permissions to a specific role, in this case to the subscriber role. In this way, when our subscribers log in to our page, they will be able to access the pages and posts that we configure as private.

We will add this code in a plugin that we have created. Remember that to modify the code of any plugin in WordPress, it is convenient that we deactivate it first. Then we will make the changes, and once the editing is finished, we save and activate it again. So we are going to do this with our plugin using the following code:

// Add read permissions to private pages and posts for any user with subscriber role. function wp_access_private_content() { global $wp_roles; // Obtain the subscriber role in a variable. $role = get_role(‘subscriber’); // We add the read permission to private pages. $role->add_cap(‘read_private_pages’); // We also include the read permission to private posts. $role->add_cap(‘read_private_posts’); } // Call to our function. add_action ( ‘admin_init’, ‘wp_access_private_content’ );

What this code does is take the subscriber role (“subscriber”), and grant it special read permissions, specifically it will allow subscribers to read the content of private pages and articles. We could use only one of the two options if we are interested. As we see in the code, this is done through the “add_cap” function.

See also  Content Network - .com

Once our plugin is activated we can test it. By logging in as subscribers, we will see that we now have access to our private page and can see all its content. If the subscriber does not log in, they will not be able to see the content of our page. If the user who logs in has the author role, they will not be able to see it either. This special permission must be expressly granted.

Therefore, the same thing that we have done with the subscriber role, we could do it with the author or collaborator role. To do this, we would simply have to replace “subscriber” (subscriber role) with “author” (author role) or “contributor” (contributor role).

Just a note here. Be careful when typing the name of the role. If we write a role that does not exist, it is possible that an error will appear and we will not be able to access our WordPress desktop. To solve it you will simply have to access the file manager of your hosting, go to the WordPress installation that you are modifying, and change the name of the plugin folder that we have just created.

Once done, we edit the .php file with the correct code, and you return to restore the name of the folder of your plugin. With these simple steps you will have access to your panel again.

Restore permissions

Keep in mind that the add_cap function will record the permission in our database. In other words, this new permission that we are granting to the subscriber will be stored and associated with the role to which we have linked it. In the case of the example, the reading permission for private pages and posts will be saved in our subscriber role.

This means that if we deactivate or even delete our plugin, subscribers who log in to our website will still be able to see the private content. They’re going to continue with that associated permit.

See also  After Effects Course #7. Animated texts - .com

So if at some point we want to remove those permissions from subscribers, or from another user role, we will have to modify our plugin and change the “add_cap” function to “remove_cap”, as we can see below. Remember to make the changes with the plugin deactivated, and reactivate it when finished.

// Withdraw special read permissions to users with subscriber role. function wp_restrict_access_private_content() { global $wp_roles; // Obtain the subscriber role in a variable. $role = get_role(‘subscriber’); // We remove the read permission to private pages. $role->remove_cap(‘read_private_pages’); // We also remove the read permission to private posts. $role->remove_cap(‘read_private_posts’); } // Call to our function. add_action ( ‘admin_init’, ‘wp_restrict_access_private_content’ );

We could do the same with any of the other roles. If we inadvertently delete a permission, we can restore it using “add_cap” as we saw at the beginning.

And that’s all! With these simple steps you can manage to share public and private content depending on the role of your users.

Summary and conclusion

In many cases it can be useful to offer extra content to our users. We can do it by assigning a password to our post or pages, and also through code if we want to show our private content to a specific role or roles.

To do this we will have to mark the pages and posts that we want as “Private” and add a small piece of code with which we grant extra reading permissions to our subscribers, collaborators or authors as we wish. The process is very simple. The possibilities to modify WordPress through plugins or custom code are endless.

And here is today’s tutorial. If you want to learn more, we recommend the . You will have access to 6047 videos about WordPress, Membership sites, plugins, development and many other interesting topics.

Loading Facebook Comments ...
Loading Disqus Comments ...