Display Content for a Specific Role - Drupal


Tags:

Sometimes, is can be a requirement to only allowed a specific group of users to view some content on a page. I'm not talking about showing or hiding the entire page to a specific role.

What I am talking about is that an authenticated user sees some different or additional content than an anonymous user will see on the same page.

I recently had a requirement to do just this on a community website that I am building. I needed to show a discount Coupon Code to registered users but not to anonymous users.

This is easily achieved by changing the content type to PHP and inserting the following snippet of code.

<?php
global $user;
$approved_roles = array('authenticated user', 'super user');

if (
is_array($user->roles)) {
  if (
count(array_intersect($user->roles, $approved_roles)) > 0) {
    print
'<p>Use Coupon Code: <strong>My Code</strong></p>When <a href="http://www.example.com">Ordering Products</a>';
  }

  else {
    print
'<p><a href="/user/login?destination=current-page-url">Login</a> | <a href="/user/register">Register</a><br /> To get Coupon Code.</p>';

}}
?>

Just a few things to note about the about PHP snippet.

  • This allows users in the "super user" and "authenticated user" groups to see the coupon code.
  • Anonymous users are prompted to Register or login to see the Coupon Code.
  • The "current-page-url" should be replaced with the url of the current page. This will allow a registered user to redirect straight back to the same page after logging in.

Thanks for the snippet!

This turned out to be handy little piece of code that works flawlessly on my drupal site . Thanks bunches!

Submitted by Bob Butterworth (not verified) on Sat, 2009-02-07 08:33.

can you please tell me where

can you please tell me where do i have to insert this code

thnx in advance

Submitted by Anonymous (not verified) on Thu, 2009-03-19 23:47.

can you please tell me where

can you please tell me where do i have to insert this code

thnx in advance

Submitted by Anonymous (not verified) on Thu, 2009-03-19 23:48.

You can insert it wherever

You can insert it wherever you want, on all pages.
Just be sure to replace
/user/login?destination=current-page-url
by the URL of the page.

Submitted by Anonymous (not verified) on Sat, 2009-03-21 01:43.

Thank you very much

I just want to let you know that you really helped me with some issues for my current project!!!!! Thank you very much

Submitted by Tobias (not verified) on Sat, 2009-06-13 01:35.

This seems an excellent

This seems an excellent features to increase overall user experience and by the way to save some of our resources. An interesting features, will give it a try. Thanks for Share.

Submitted by Custom Website Development (not verified) on Wed, 2011-02-23 20:11.

I read your blog all the time

I read your blog all the time and I just thought I’d say keep up the terrific work!

Submitted by mercedesbenz (not verified) on Fri, 2011-05-27 14:30.

This is the best example of

This is the best example of advancement in technology and development. I too had a similar requirement from one of my client but was unable to find a way, here it seems now I can accomplish the same task.

Submitted by Salvage Cars (not verified) on Sat, 2011-06-04 03:37.

i found this tutorial very

i found this tutorial very informative and i like the way you described it :)

Submitted by stunning boy (not verified) on Wed, 2011-06-22 08:14.

Thanks for this useful

Thanks for this useful content for Drupal. I helped me a lot in my work.

Submitted by enterprise app integration (not verified) on Sun, 2011-11-06 13:14.