- This topic has 6 replies, 4 voices, and was last updated 3 years, 3 months ago by
Ying.
-
AuthorPosts
-
January 21, 2023 at 3:31 pm #2504341
Carsten
Hi there, I’m looking for away to make a conditional tag for user roles and have been looking through the WP Conditional Tags Codex without finding anything about user roles.
What I’m trying to achieve is to display this shortcode for customers
add_action( 'bp_after_member_profile_content', 'konkurrence_shortcode' ); function konkurrence_shortcode() { echo do_shortcode( '[nfpd_entry_page]' ); }I have been trying with
if ( is_customer() )andif(current_user_is("customer"))without luck.Why is user roles not on the conditional tags list, is this not possible?
Thanks
January 22, 2023 at 7:36 am #2504746David
StaffCustomer SupportHi there,
Wordpress haas the
wp_get_current_user()function:
https://developer.wordpress.org/reference/functions/wp_get_current_user/And that includes the
rolesarray which you can check for your specific role.Example code to check us user has a role of customer:
$user = wp_get_current_user(); if ( in_array( 'customer', (array) $user->roles ) ) { //The user has the "customer" role }January 22, 2023 at 4:49 pm #2505217Scott
Does this help?
$user = wp_get_current_user(); if ( in_array( 'shop_manager', (array) $user->roles ) ) { // user role condition echo do_shortcode('[gravityform id="2"]'); }January 23, 2023 at 9:29 am #2506133Carsten
Yes, it’s working very well now, thank you both 😉
One question, I’m trying to add two user roles to the code, I have tested it in php checker, with no issues found, but when i insert it in the code, the site breaks
add_action( 'bp_after_member_profile_content', 'konkurrence_shortcode' ); function konkurrence_shortcode() { $user = wp_get_current_user(); if ( in_array( 'contributor','subscriber', (array) $user->roles ) ) { // user role condition echo do_shortcode( '[nfpd_entry_page]' ); } }January 23, 2023 at 12:14 pm #2506304Ying
StaffCustomer SupportTry this:
add_action( 'bp_after_member_profile_content', 'konkurrence_shortcode' ); function konkurrence_shortcode() { $user = wp_get_current_user(); if ( in_array( 'contributor', (array) $user->roles ) || in_array( 'subscriber', (array) $user->roles )) { echo do_shortcode( '[nfpd_entry_page]' ); } }January 23, 2023 at 12:42 pm #2506326Carsten
Thanks Ying 😉
January 23, 2023 at 12:45 pm #2506335Ying
StaffCustomer SupportNo Problem 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.