Skip Navigation

[Closed] Testing User Role to Conditionally Display Content

This support ticket is created 10 years, 8 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.

This topic contains 8 replies, has 2 voices.

Last updated by Bigul 10 years, 8 months ago.

Assisted by: Bigul.

Author
Posts
#60733

I have seen topic https://toolset.com/forums/topic/hide-edit-link/ and understand the concept of using the user role to decided whether of not to display a form or other content on a page. I have also checked the wordpress links but find that information difficult to relate.

My question is, how do I modify the answer to different user role situations.

The example given tests for Admin level, how is it modified to test subscriber, editor, contributor etc.

Does this function do the job:

===
function is_role_group_func($atts=array(), $content=null)
{
extract( shortcode_atts( array(
'role' => '',
), $atts ) );
if(current_user_can($role))
{
return do_shortcode($content);
}
return false;
}
add_shortcode('is_role_group', 'is_role_group_func');
====

If so, which bit of code needs changing in this

===
Edit your views template, for example, we display 12345, if current user role is "admin":
[is_role_group role="remove_users"][cred-link-form form="My form" text="Edit %TITLE%" target="_self"][/is_role_group]
===

Sorry if this is a bit of a basic question but I have almost zero php skills and I am sure this will interest others.

Thanks
Tony

PS
Testing for user being logged in is fine - I use that already, I just want to be able to introduce other cut-off points for specific user roles.

#60886

Bigul
Supporter

Hi Tony,

Please confirm you are using our Access( https://toolset.com/documentation/user-guides/#Access ) plug-in for permission management or not ?

Can you please explain little more about your exact purpose to avoid confusions.

---
With Regards

Bigul

#60945

Yes - I am using Access 1.1.3 and it's activated.

When I create a web page I have some information that is open for all to see and other information that should only be visible to logged in users with a designated role or higher.

So my code will look like:

=====================

info for all to see

[admin only shortcode]----info only admin details---[/admin only shortcode]

info for all to see

[editor plus only shortcode]i=---Info only editor details---[/editor plus only shortcode]

[contributor plus only shortcode]---info only contributor details---[/contributor plus only shortcode]

info for all to see

=====================

As long as I can relate the user to a control role level the exact method does not matter.

In the database data will be organised so that is is displayed in its approriate section. So a conact email may be available to anyone logged in and an edit Cred form may be editor upwards and a list of names may be visible to logged in users upwards.

I hope this helps - Access will not do this at a field level as far as I am aware.

Thanks
Tony

#61193

Bigul
Supporter

Hi Tony,

Hope this will help. Create a function in theme's functions.php to get User Role. Then create a short code for that function. Finally call that function in [wpv-if] condition, evaluate the user role and display relevant result for each user.

Please check following link for more details.

https://toolset.com/documentation/views-shortcodes/#wpv-if

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

----
With Regards

Bigul

#61206

Hi Bigul

I am not quite following all this. Does the function I included (supplied by Luo) get the user role? If so, how do we test something like "=contributor or greater)?

What does shortcode look like?

What would [wpv-if] look like.

So, in summary what are all the bits and syntax to do this:

1. get user role
2. test if contributor or greater, if so display "XXXXXX"

I understand the principle, but getting the right bits in place with the right syntax is not always clear. I am building my own functions and "how to" list in generic terms so I can modify it for whatever conditions I need. All pretty basic but mysterious to non php programmers!

Thanks
Tony

#61218

Bigul
Supporter

Hi Tony,

Please place this code in your functions.php of your theme's folder.

function get_wp_user_role() {
global $current_user;

$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);

return $user_role;
}
add_shortcode('wpv-post-get-wp-user-role', 'get_wp_user_role');

Then use following wpv-if condition in your Views.

[wpv-if evaluate=" '[wpv-post-get-wp-user-role]' = 'administrator' "]
This is a Page For administrator
[/wpv-if]
[wpv-if evaluate=" '[wpv-post-get-wp-user-role]' = 'editor' "]
This is a Page For Editor
[/wpv-if]
[wpv-if evaluate=" '[wpv-post-get-wp-user-role]' = 'author' "]
This is a Page For Author
[/wpv-if]

Hope it will work.

----
With Regards

Bigul

#61244

Hi Bigul

Yes that worked for specific roles - many thanks for that.

One extra question, is it possible to show the display for the named role and higher levels?

I want to allow editor to see his and all lower level content so Admin sees all, editor sees all except admin etc.

If that can be done it can save some coding on the page.

Thanks
Tony

#61248

Sorry, I mean't to add that I am aware that I can do this:

[wpv-if evaluate=" '[wpv-post-get-wp-user-role]' = 'editor' OR '[wpv-post-get-wp-user-role]' = 'administrator' "]
This is a Page For Editor and above
[/wpv-if]

Just looking for a better solution, if there is one.

Tony

#61827

Bigul
Supporter

The topic ‘[Closed] Testing User Role to Conditionally Display Content’ is closed to new replies.