Multisite Issue
Support › MB User Avatar › Multisite IssueResolved
- This topic has 6 replies, 2 voices, and was last updated 3 years, 3 months ago by
Nic.
-
AuthorPosts
-
November 10, 2021 at 11:28 PM #31850
Nic
ParticipantHello,
I love that you have this plugin! It is desperately needed. However, I noticed that the user image will not sync on subsites in a multisite setup. I feel this is due to the file being stored in the main sites media folder, but not subsites. While I might be able to resolve this with a multisite shared media plugin, that is redundant and wastes server space.
Is there another workaround for this so that avatar images show up on multisite subsites?
November 11, 2021 at 11:01 AM #31864Long Nguyen
ModeratorHi,
Please refer to this topic to get the field value from the main site and display it on the subsites with coding
https://support.metabox.io/topic/display-fileds-from-network-settings-page/December 24, 2021 at 5:52 AM #32849Nic
ParticipantLong,
Thank you for your helpful reply 🙂 I have been trying to figure this out based on the settings page reference you gave and the tutorial here: https://metabox.io/create-custom-avatar/
I have the upload field and image working beautifully on my main site but cannot seem to get the code to work to show avatars on my subsites. This is also an issue with the MB User Avatar v1.0 plugin you offer. Do you see anything in my code below that I am doing wrong?
add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 ); function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt ) { switch_to_blog( 1 ); $user = false; if ( is_numeric( $id_or_email ) ) { $id = (int) $id_or_email; $user = get_user_by( 'id' , $id ); } elseif ( is_object( $id_or_email ) ) { if ( ! empty( $id_or_email->user_id ) ) { $id = (int) $id_or_email->user_id; $user = get_user_by( 'id' , $id ); } } else { $user = get_user_by( 'email', $id_or_email ); } if ( $user && is_object( $user ) ) { $value = rwmb_meta( 'custom_avatar', array( 'object_type' => 'user' ), $user->data->ID ); if ( $value ) { $avatar = "<img src='" . $value['url'] . "' class='avatar avatar-" . $size . " photo' alt='" . $alt . "' height='" . $size . "' width='" . $size . "' />"; } } return $avatar; } restore_current_blog();
December 24, 2021 at 10:36 PM #32855Long Nguyen
ModeratorHi,
Did you try to use this sample code to get the image via
guid
?
https://pastebin.com/txYEyDvcDecember 25, 2021 at 12:00 AM #32859Nic
ParticipantYes, I cannot seem to get that to work. The example code also uses groups and settings pages which don't seem to translate for me here.
December 25, 2021 at 11:28 PM #32865Long Nguyen
ModeratorHi,
Here is the code that works on my end.
add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 ); function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt ) { $user = false; if ( is_numeric( $id_or_email ) ) { $id = (int) $id_or_email; $user = get_user_by( 'id' , $id ); } elseif ( is_object( $id_or_email ) ) { if ( ! empty( $id_or_email->user_id ) ) { $id = (int) $id_or_email->user_id; $user = get_user_by( 'id' , $id ); } } else { $user = get_user_by( 'email', $id_or_email ); } if ( $user && is_object( $user ) ) { $id = get_main_site_id(); switch_to_blog( $id ); $value = rwmb_meta( 'custom_avatar', array( 'object_type' => 'user' ), $user->ID ); //return image ID $image = get_post( $value ); //get post by image ID if ( $value ) { $avatar = "<img src='" . $image->guid . "' class='avatar avatar-" . $size . " photo' alt='" . $alt . "' height='" . $size . "' width='" . $size . "' />"; } } restore_current_blog(); return $avatar; }
You can add the code in the sub-site to get and show the user meta from the main site.
December 27, 2021 at 10:50 PM #32882Nic
ParticipantAha! That did it! You are a wizard, thank you so much 😀
For anyone referring to this thread in the future, I had to create two separate snippets to get this to work.
First, for my main site I activated and used the exact snippet and instructions found in the tutorial here: https://metabox.io/create-custom-avatar/
Second, I used the snippet provided by Long just above and activated it on all my subsites.
Tip: If you don't have a way to manage your multisite snippets already, I used the free plugin from the WordPress repository called "snippets" because it allowed me to manage my snippets on my network site and then make them available across the network. I can choose which snippets to activate on each subsite.
So, code snippet #1 from the tutorial is active on my main network site. Snippet #2 provided by Long above is activated on all my subsites.
-
AuthorPosts
- You must be logged in to reply to this topic.