The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Hide Content Based On User Role with Shortcode

Labels

This Discussion is public

Notifications

If you're using a membership plugin to manage your roles, you will probably already have the necessary shortcode(s) to hide/show content based on the user's role or capability. However, if you are using roles and capabilities WITHOUT using a full blown membership plugin, you can easily create your own shortcodes.

To create a shortcode that hides the content you place between the opening and closing tags of your shortcode, add the following to you theme function.php or, better yet, to a simple custom plugin...


// add private content via shortcode
function private_content($atts, $content = null) {
if (current_user_can('my_role')) //replace my_role with the name of the role you want to see content
return do_shortcode('
' . $content . '
'); return ''; } add_shortcode('my_private_content', 'private_content');

Notes:

  • You can use either a role or a capability that WP creates or you created
  • If using roles, all roles above the one you set will also see the content
  • This code is optimized for Formidable Pro by including the do_shortcode() function so you can add FP shortcodes to the hidden content
  • The content is wrapped in a class so you can style the hidden content if you want

You can also use my plugin, which has a robust set of options, including ability to specify multiple usernames, user roles, capability levels, logged in/out status, and any combination of those, and whether to show or hide the content from the specified users. Eyes Only: User Access Shortcode.

I noticed your plugin while looking at solutions. Your plugin logos really stand out!

I'll use it next time I need to do more than this simple task required. I will also update the blog post that's upcoming with this tip to mention your plugin as a more robust alternative.

Hey, thanks very much. Your function, of course, has the advantage of being considerably more lightweight.

All the best.

Thomstark, thank you very much for your plugin, it works like a charm.

good morning
I see that this plugin is out of date, was abandoned?

not tested with wordpress 4.2.2 ..

Thank you for your reply.

Still works with 4.2.2. Just haven't updated the readme lately.

Discussion closed.