Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Detecting Multiple Roles Capacity WordPress

  • SOLVED

Should be a fairly easy one...

The code below detects the user role and allows me to show or hide content...

<?php if($user->has_cap('s2member_level1')) { // Custom fields only display to Level 1 members ?>

However, what i need is the same capability but with multiple roles... i.e. if user has capacity as either subscriber or s2member_level 1 then show or hide something...

I tried the following below without success...

<?php if($user->has_cap('subscriber|'s2member_level1')) { // Custom fields only display to Level 1 members & Subscribers ?>

<?php if($user->has_cap('subscriber', 's2member_level1')) { // Custom fields only display to Level 1 members & Subscribers ?>

Thanks,
Sean.

Answers (1)

2014-01-12

Bob answers:

you can try something like this

if($user->has_cap('subscriber') || $user->has_cap('s2member_level1'))


Sean Gartlan comments:

Bhavesh,


A little bit of background...

I am have created some custom meta fields in the wordpress backend profile area... The code discussed will identify the capacity of a user and display differing fields for the user to update... In this instance these two user roles get the same custom meta fields...

So I applied your suggestion and it while it works in determining the role capacity and which fields they should see when I try to save the profile in the wordpress backend I get a white screen...

This is a snippet of the save function... Could the if ( !current_user_can( 'edit_user', $user_id ) ) be conflicting with the if($user->has_cap('subscriber') || $user->has_cap('s2member_level1'))

<?php } }

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )

return false;

if(!empty( $_POST['business_name'])){

update_usermeta( $user_id, 'business_name', $_POST['business_name'] );

}

etc. etc.

Thanks,
Sean.


Sean Gartlan comments:

Bhavesh,

Turned debug on and found where the problem was... I was using a if ( user_can( $current_user, "subscriber")) on my meta field error reporting and not has_cap

So I used your same syntax on the user_can

if ( user_can( $current_user, "subscriber") || user_can( $current_user, "s2member_level1"))

and it all works fine...

Thanks for the good answer...

Cheers,
Sean.