Skip Navigation

[Resolved] Multi Site: Display Content on Site #1 Based on User’s Role on Site #3

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

Problem: In site A of a multisite installation, I would like to create a conditional that will test the current User's role in site B.

Solution: Create a custom shortcode that will allow you to access user information in a specific site on your network.

Relevant Documentation: https://wordpress.stackexchange.com/questions/163262/how-to-get-a-user-role-of-a-specific-blog-in-multisite

This support ticket is created 7 years, 3 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)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by julieP 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#558237

I have a multi site install. Users register on the main site (Site #1) and have role called Individual. When they visit any of the other sites, they are automatically given the role of IndividualX on that site where X is the site number (this is achieved via an add_action function).

I'd like to be able to display content to Individuals on Site #1 depending on which sites they have this additional role on.

If I look to creating a View on Site #1 based on Users and their role, the only option available is to base the output on wp_capabilities which uses the role of the User on Site #1. I'm not able to use any usermeta that relates to any of the other sites even though usermeta isn't site specific (by that I mean all usermeta stays together; there is no usermeta in the site-specific tables in the database).

Is there a way of also including the capabilities on the other sites in the argument? Essentially I'm hoping to be able to say if the User's role on Site#1 is Individual, display ABC, if the User's role on Site#3 is Individual3, display XYZ.

Many thanks

PS: Any chance Christian or Nigel could take a look at this one please?

#558270

Hello, the only way to do this is to create a custom shortcode that checks a User's role based on the site ID and user ID, using the WP_User class, or get_users(). Check out this post for some code samples of these methods:
https://wordpress.stackexchange.com/questions/163262/how-to-get-a-user-role-of-a-specific-blog-in-multisite
- Your shortcode can return "true" if the role is found, or return nothing if it is not found.
- Register your shortcode in Toolset > Settings > Front-end Content > Third-party shortcode arguments, so you can use this shortcode in a conditional.
- Your conditional should test whether the new shortcode is equal to "true". If so, output XYZ.
- Copy and paste your conditional again, and add evaluate="false" in the conditional shortcode. This tests the opposite condition, so change the output to ABC.

Make sense? Need help with any of these steps?

#558483

hi Christian

Many thanks for picking this one up for me ????

I understand how this would work but implementing it is another story! I've learnt loads since using Toolset but my coding skills are desperately lacking. Nevertheless I had a look at your linked page but to be honest am out of my depth on this one. I've spent a couple of hours googling generally and trying out a few things but not quite achieving the desired results.

For example, I found this but it only looks at the site/blog the user is currently visiting:-

add_shortcode('user_role', 'show_user_role');

function show_user_role() {
    global $wp_roles;

    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    $role = array_shift( $roles );

    return isset( $wp_roles->role_names[ $role ] ) ? translate_user_role( $wp_roles->role_names[ $role ] ) : FALSE;
}

I also tried these shortcodes as I thought this might actually be a 'shorter' route to the required output:-

add_shortcode('blog_4', 'show_content_4');

function show_content_4() {
if (current_user_can_for_blog('4', 'edit_others_posts')) {
return 'ABC';
}
}

add_shortcode('blog_5', 'show_content_5');

function show_content_5() {
if (current_user_can_for_blog('5', 'edit_others_posts')) {
return 'XYZ';
}
}

but when I tested it on the frontend as a User (with Editor-based custom role) , I can see ABC and XYZ even though the User has only been added to Blog #4. A bit odd.

I did submit this thread at this point and after a bit more detective work I came up with this:-

add_shortcode('blog_4', 'show_content_4');

function show_content_4() {
$user_id = get_current_user_id();
$blog_id = 4;

if ( is_user_member_of_blog( $user_id, $blog_id ) ) {

  return 'ABC';
}
}

add_shortcode('blog_5', 'show_content_5');

function show_content_5() {
$user_id = get_current_user_id();
$blog_id = 5;

if ( is_user_member_of_blog( $user_id, $blog_id ) ) {

  return 'XYZ';
}
}

This is producing the correct output according to the sites the User belongs to. As you're the expert here, would you say this approach is an acceptable one ?

Many thanks Christian

#558638

I would caution that you're no longer testing the user role on each of these other sites, only testing if the user is a member. If you're okay with that, seems okay to me. However, there's always the chance that a user's role could change later so if you want to be specific about IndividualX role, your conditional needs to be more specific.

It also seems more efficient to create one shortcode that can use variables instead of a shortcode for each blog. For example, you can use a shortcode attribute to provide a dynamic blog ID:

[wpv_user_is_member blog="2"]

Then you can access that "blog" value in your shortcode callback. This way you don't need to write custom code for each blog. This is kind of "nice to have", not necessary though. More info about shortcode attributes:
https://codex.wordpress.org/Shortcode_API#Handling_Attributes

#559132

Hi Christian

Thanks for pointing this out; it's a very valid observation.

I've given it some thought and come to the conclusion that for this particular site, it's fine to base things on a User's member status rather than their role.

The situation may be different for other sites of course and I appreciate you prompting me to consider this. Hopefully I'll have improved my coding skills my then!

Thanks again for your help.