Skip Navigation

[Resolved] showing elements based on used role (Access, Conditional Display, User Role)

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

Problem:

I am trying to show a stamp using conditional display based on the post author role on the website,
for example : admin, dealer, editor , or any authorized users will have a stamp after the user account on a post, where other users or visitors won't, this is really important to the website concept.

I am trying to do it using wpv-post-user, role and condition display,

Solution:

You are right, it is not possible within Views built-in shortcodes, but you can create another custom shortcode to get the user role of post author, then use it into [wpv-conditional] shortcode, for example there is a solution in another similar thread:
https://toolset.com/forums/topic/conditional-display-based-on-user-role-of-post-author/#post-543582

For your reference

Relevant Documentation:

https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 6 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Author
Posts
#620379
Capture3.JPG
Capture2.JPG
Capture.JPG

Hi,

I am trying to show a stamp using conditional display based on the post author role on the website,
for example : admin, dealer, editor , or any authorized users will have a stamp after the user account on a post, where other users or visitors won't, this is really important to the website concept.

I am trying to do it using wpv-post-user, role and condition display,

attached snapshots,

any help will be appreciated,

thanks

PS: I will be creating different user forms, where each will assign any registration to different roles, for example: a user form for editiors, when a user sign up with that form will be assigned an editor role.

thanks again

#620385

Hi,

You are right, it is not possible within Views built-in shortcodes, but you can create another custom shortcode to get the user role of post author, then use it into [wpv-conditional] shortcode, for example there is a solution in another similar thread:
https://toolset.com/forums/topic/conditional-display-based-on-user-role-of-post-author/#post-543582

For your reference

#620399

Thank you so much ! that worked,
one more question, I am entering the conditions manually, like :

 
[wpv-conditional if="'[user_role_func]' eq 'administrator'"]                
  <!--  Verification Stamp code  -->
   // content
<!--  Verification Stamp code  -->
[/wpv-conditional]

a) how would I modify the condition to include multiple roles, comma spattered ? or using OR

 
[wpv-conditional if="'[user_role_func]' eq 'administrator' OR '[user_role_func]' eq 'editior' OR '[user_role_func]' eq 'dealer' "]                
  <!--  Verification Stamp code  -->
   // content
<!--  Verification Stamp code  -->
[/wpv-conditional]

b) what should be the non-logged-in or visitors roles be ?

Thanks a lot

#620424

a) how would I modify the condition to include multiple roles, comma spattered ? or using OR
Yes, you can use OR logic as the condition, for example:

[wpv-conditional if="('[user_role_func]' eq 'administrator') OR ('[user_role_func]' eq 'editior') OR ('[user_role_func]' eq 'dealer') "]                
  <!--  Verification Stamp code  -->
   // content
<!--  Verification Stamp code  -->
[/wpv-conditional]

More help:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#syntax
Boolean operators: AND, OR, NOT

b) what should be the non-logged-in or visitors roles be ?
I tested it in my localhost, if the post author is a non-logged-in or visitors, that shortcode [user_role_func] will trigger PHP errors, but you can modify the PHP codes as below:

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

Then check if it is visitor like this:

[wpv-conditional if="'[user_role_func]' eq 'visitor'"]                
  <!--  Verification Stamp code  -->
   // content
<!--  Verification Stamp code  -->
[/wpv-conditional]
#620503
visitor in Views Slider.JPG
visitor in Layout.JPG

Thank you Luo, that worked just like perfect , except for minor issue, it works great in Layouts but in a Search view or View Slider, it shows an empty space instead, I added snapshots, if there is no way around, I can just assign a class for that div and using custom js to add it manually.

#620533

I was able to solve it in view using another way since it shows an empty space for visitors, and since I will be triggering all other roles, by using :

[wpv-conditional if="'[wpv-post-author]' eq ''"]                
   visitor
[/wpv-conditional]

if there is a better stable way using php I would like it, otherwise I am so much thankful for the help Luo!

#620786

Since your screenshot isn't in English, I am not sure which one is the "empty space for visitors", where is it from?
It works fine in my localhost, if you still need assistance for it, please try these:
1) deactivate other plugins and switch to wordpress default theme, and test again
2) If the problem still persists, please provide a database dump file (ZIP file) of your website in below private detail box, I need to test and debug it in my localhost, thanks

#622583

Hi Luo, Thank you for the reply,and sorry for the confusion, I think I have solved using the following :

[wpv-conditional if="'[wpv-post-author]' eq ''"]                
   visitor
[/wpv-conditional]

and since I will try to track all the other roles, this should and has been working fine.

thanks again