Skip Navigation

[Resolved] Conditional output if wpv-post-author is a administrator or other roles

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

Problem:

I'm creating a membership website with three membership options. Free, Silver and Gold. Each membership is attached to a role in WordPress. I am using another plugin "multiple-roles", each user can have multiple user roles in your website.

Now I want to show the member profil information depending if has one of these roles.

Solution:

I suggest you create another shortcode [author-has-role] for it, for example:

https://toolset.com/forums/topic/conditional-output-if-wpv-post-author-is-a-administrator-or-other-roles/#post-1231330

Relevant Documentation:

This support ticket is created 5 years 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)

This topic contains 4 replies, has 2 voices.

Last updated by jean-francoisB 5 years ago.

Assisted by: Luo Yang.

Author
Posts
#1230287

Hi Luo,

You have been helping me on this topic earlier and this is the thread:

https://toolset.com/forums/topic/conditional-output-if-wpv-post-author-is-a-administrator/

I have tried what you tell me to do and it's work find for the administrator role, but doesn't work for any other role that I created.

I'm creating a membership website with three membership options. Free, Silver and Gold. Each membership is attached to a role in WordPress. I have created those roles with ToolSet Access.

Now I want to show the member profil information depending if has one of these roles. This is an example :

[wpv-conditional if="( '[user_role_func]' eq 'silver_membership' ) OR ( '[user_role_func]' eq 'gold_membership' )"]
<div class="bio">Bio : [types field='bio'][/types]</div>
[/wpv-conditional]

So that means that if the actual author of this profile has a silver membership OR a gold membership, you can show that information to the public. That means that this information won't be showed on all FREE membership profile.

Actually, that doesn't work. This condition only work with [user_role_func]' eq 'administrator' ).

Is there a way to make it works for all roles that I have created?

I'm not sure what I'm doing wrong here.

Thanks for your help!

#1230778

Hello,

Thanks for the details, I can login into your website.

Please point out the URLs of your website:
- Where I can edit your custom PHP codes
- Where I can test the shortcodes in front-end

Thanks

#1231011

Hi Luo,

- Where I can edit your custom PHP codes

You can go on the left in Settings -- PHP inserter. You will find the function to get the author role there (function get_author_role())

- Where I can test the shortcodes in front-end

I'm using this shortcodes in my ToolSet Views "Listing View (single)". But you will need to create your own listing first by going into "My account" at the top and looking for "listing" on the left side. You fill the fields and you will be able to see your listing in "Find a practitioner" in the top menu. Or you can also just look at my listing "OK Web Design". I have put myself all the roles (admin, FREE membership, SILVER membership and GOLD membership). You can also attribute yourself other roles to try the function.

Thank you very much for your help!

#1231330

Thanks for the details, WordPress can setup only one role for each user by default, but you are using another plugin "multiple-roles", each user can have multiple user roles in your website.

So the custom shortcode [user_role_func] won't work as expected in your case.

I suggest you create another shortcode [author-has-role] for it, for example:
1) Add below PHP codes into your theme file "functions.php":

add_shortcode('author-has-role', function($atts, $content){
	$atts = shortcode_atts( array(
		'role' => '',
	), $atts);
    global $authordata;
    $author_roles = $authordata->roles;
	$res = 0;
	if ( $atts['role'] && in_array($atts['role'], $author_roles) ){
		$res = 1;
	}
	return $res;
});

2) Dashboard-> Toolset-> Settings-> Front-end Content
in section "Third-party shortcode arguments", add the new shortcode name: author-has-role

3) Use above shortcode like this:

[wpv-conditional if="( '[author-has-role role="administrator"]' eq 1 ) OR ( '[author-has-role role="subscriber"]' eq 1 )"]
the author has one of those roles: administrator, subscriber
[/wpv-conditional]
#1231593

This is perfect! Exactly what I needed! Works like a charm!

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.