Skip Navigation

[Resolved] Conditional display based on user role of post author

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display content based on the User Role of the Post Author?

Solution:
1. Please add following code in your theme’s or child theme’s functions.php file:

function get_author_role()
{
    global $authordata;
  
    $author_roles = $authordata->roles;
    $author_role = array_shift($author_roles);
    return $author_role;
}  
add_shortcode( 'user_role_func', 'get_author_role' );

2. Register the ‘'user_role_func'’ shortcode first in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments. Screenshot here: https://d7j863fr5jhrr.cloudfront.net/wp-content/uploads/2017/06/543582-Register_Custom_Shortcode.png?x36781

3. Then use shortcode in conditional output something like this, you can add it in your View or Content Template:

[wpv-conditional if="( '[user_role_func]' eq 'contributor' )"]
contributor content goes here 
[/wpv-conditional] 
This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by David 5 years, 5 months ago.

Assigned support staff: Noman.

Author
Posts
#543434

I am trying to write a condition (using shortcode) to display content based on the user role of the post author.

I am using Access to created my user role.

Something like:

if post author equals user role...

Is there a shortcode for getting the user role?

I found this simliar thread:
https://toolset.com/forums/topic/toolset_access-roleadministrator-operatorallowtoolset_access-in-views/

But that gets user role of logged in user. I want user role of post author in a content template of a post.

#543582

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Register Custom Shortcode.png

Hi David,

Thank you for getting in touch with us. This is not possible using builtin shortcodes, but I have created a custom shortcode for you which you can use:

1. Please add following code in your theme’s or child theme’s functions.php file:

function get_author_role()
{
    global $authordata;
 
    $author_roles = $authordata->roles;
    $author_role = array_shift($author_roles);
 
    return $author_role;
}
 
add_shortcode( 'user_role_func', 'get_author_role' );

2. Register the ‘'user_role_func'’ shortcode first in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments (see attached screenshot: Register custom shortcode.png)

3. Then use shortcode in conditional output something like this, you can add it in your View or Content Template:

[wpv-conditional if="( '[user_role_func]' eq 'contributor' )"]
contributor content goes here 
[/wpv-conditional]

Thank you

#544754

Thanks Norman, that works nicely.