Jan 202014

As WordPress itself is very powerful concerning permission management, it’s sometimes a little bit difficult to perform very simple tasks. Recently I wanted to display different contents for different users (or more specifically: different user roles). However this isn’t possible to do out of the box.
There are a lot of plugins for managing user groups (or here) and even build big membership systems. But this seemed like a complete overkill for what I wanted to do. So I wrote a small shortcode that could do exactly that.

The usage is simple: an enclosing shortcode named [user_role] will take the attribute of role=”role; the content between opening and closing shortcode brackets will only be shown to those users who have the appropriate role specified in the attribute.

The user roles supported by WordPress by default are subscriber, contributor, author, editor and administrator. Of course you can expand this list by using various plugins (e.g. this one) and the shortcode will also work on those.

In order for the above shortcode to work, you will need to place the following PHP code in the functions.php of your current theme (you can do this of course via SSH, FTP or in the WordPress administrator backend editor):

Again, this was meant to perform just a simple task, that’s why it was done as a featureless and minimalistic piece of coding.
One of the features that could be added next would be the possibility to use a list of user roles as attribute, because at this stage, you have to write one shortcode for each user role you would like to read the content (even if it’s the same for different user roles).

  2 Responses to “WordPress shortcode for user role visibility”

  1. Thanks man! Works nicely

  2. Thank you! It works!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">