Skip Navigation

[Resolved] Check user role/capabilities to display content

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

Problem: I would like to create a conditional that tests whether or not the current logged-in User has a specific capability.

Solution: Use the built-in WordPress function current_user_can to check for any individual capability. You must register current_user_can in Toolset > Settings > Front-end Content > Functions inside conditional evaluations, then you can do something like this:

[wpv-conditional if="(current_user_can('access_s2member_ccap_4792') eq 1)"]
current user has capability access_s2member_ccap_4792
[/wpv-conditional]
 
[wpv-conditional if="(current_user_can('access_s2member_ccap_4792') ne 1)"]
current user does not have capability access_s2member_ccap_4792
[/wpv-conditional]

Relevant Documentation:
https://developer.wordpress.org/reference/functions/current_user_can/

100% of people find this useful.

This support ticket is created 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by dmitryK-2 2 years, 9 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#1590855
Screenshot 2020-04-18 at 17.41.57.png
Pic2 2020-04-18 at 16.18.17.png
Pic1 2020-04-18 at 16.18.39.png

Tell us what you are trying to do?
Hi, unfortunatelly my php is on initial level, so asking your advice what to add/change in code for shortcode to fit my logic, the basic idea of solution provided in this topic - https://toolset.com/forums/topic/testing-user-role-to-conditionally-display-content/

That solution almost fitted my check, but something is missing - I need to find capability for page (I create it with wpv-if) in array of user custom capabilities:

test page for this - permalink: hidden link

I can see, that when I add Custom Capabilities for User (see pic.1), they updated in wp db field 'meta_key'='wp_capabilities' (see pic.1 & pic.2). So my capability 'access_s2member_ccap_4791' is the same as page 'test-page'=ID 4791. But I still can't get it hidden text showing up, because php array_shift somehow lefts only role e.g. 'administrator' or 'customer' , cutting off all the capabilities.

[wpv-if evaluate=" '[wpv-post-get-s2-user-role]' = 'access_s2member_ccap_[wpv-post-id]' " debug="true"]
This is a Page For customer
[/wpv-if]

Debug result is in pic.3.

The code in functions.php of child-topic ( & registered shortcode in Toolset settings ), but looks that :

		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-s2-user-role', 'get_wp_user_role');

THANKS IN ADVANCE FOR HELP

#1591627

This code is designed to return a single role when only one User role is possible. In your case, multiple User roles are allowed so the code will not work correctly. You can adjust the code to return a comma-separated list of roles, like this:

function get_wp_user_roles() {
    global $current_user;
 
    $user_roles = implode( ',', $current_user->roles);
 
    return $user_roles;
    }
add_shortcode('wpv-post-get-s2-user-roles', 'get_wp_user_roles');

This will return a comma-separated list of roles. Then you can use the CONTAINS and ARRAY functions in a conditional to test whether or not a specific User role is included in that list.

[wpv-conditional if="(CONTAINS(ARRAY([your-shortcode]),'some-value'))"]
 ... some-value is one of the roles....
#1591827
Screenshot 2020-04-19 at 20.41.00.png
Screenshot 2020-04-19 at 20.32.34.png

Hi Cristian, thanks for your reply

I've putted in debug mode, and for my admin user have the following info:
- pic 1 for my admin user in bd last two capabilities values are entered


a:34:{s:13:"administrator";b:1;s:26:"wpcf_custom_post_type_view";b:1;s:26:"wpcf_custom_post_type_edit";b:1;s:33:"wpcf_custom_post_type_edit_others";b:1;s:25:"wpcf_custom_taxonomy_view";b:1;s:25:"wpcf_custom_taxonomy_edit";b:1;s:32:"wpcf_custom_taxonomy_edit_others";b:1;s:22:"wpcf_custom_field_view";b:1;s:22:"wpcf_custom_field_edit";b:1;s:29:"wpcf_custom_field_edit_others";b:1;s:25:"wpcf_user_meta_field_view";b:1;s:25:"wpcf_user_meta_field_edit";b:1;s:32:"wpcf_user_meta_field_edit_others";b:1;s:17:"ddl_create_layout";b:1;s:28:"ddl_assign_layout_to_content";b:1;s:15:"ddl_edit_layout";b:1;s:17:"ddl_delete_layout";b:1;s:34:"wpml_manage_translation_management";b:1;s:21:"wpml_manage_languages";b:1;s:41:"wpml_manage_theme_and_plugin_localization";b:1;s:19:"wpml_manage_support";b:1;s:36:"wpml_manage_woocommerce_multilingual";b:1;s:37:"wpml_operate_woocommerce_multilingual";b:1;s:29:"wpml_manage_media_translation";b:1;s:22:"wpml_manage_navigation";b:1;s:24:"wpml_manage_sticky_links";b:1;s:30:"wpml_manage_string_translation";b:1;s:33:"wpml_manage_translation_analytics";b:1;s:25:"wpml_manage_wp_menus_sync";b:1;s:32:"wpml_manage_taxonomy_translation";b:1;s:27:"wpml_manage_troubleshooting";b:1;s:31:"wpml_manage_translation_options";b:1;s:25:"access_s2member_ccap_4791";b:1;s:25:"access_s2member_ccap_4792";b:1;}

- pic 2 debug mode for shortcode on test page - is it correct that only admin role is shown ? than condition didn't get success result

#1592013
additional-roles.png

Okay I think you are trying to check a User's capabilities, but the code you are using is designed to check the User's role(s). Roles and capabilities are two different things. With that in mind, you should use the built-in WordPress function current_user_can to check for an individual capability. You must register current_user_can in Toolset > Settings > Front-end Content > Functions inside conditional evaluations, then you can do something like this:

[wpv-conditional if="(current_user_can('access_s2member_ccap_4792') eq 1)"]
current user has capability access_s2member_ccap_4792
[/wpv-conditional]

[wpv-conditional if="(current_user_can('access_s2member_ccap_4792') ne 1)"]
current user does not have capability access_s2member_ccap_4792
[/wpv-conditional]

Documentation for that WP API: https://developer.wordpress.org/reference/functions/current_user_can/

Does this make sense?

#1601385

My issue is resolved now. Thank you!