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
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....
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
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?
My issue is resolved now. Thank you!