Multisite Issue

Support MB User Avatar Multisite IssueResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #31850
    NicNic
    Participant

    Hello,

    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?

    #31864
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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/

    #32849
    NicNic
    Participant

    Long,

    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();
    #32855
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Did you try to use this sample code to get the image via guid?
    https://pastebin.com/txYEyDvc

    #32859
    NicNic
    Participant

    Yes, 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.

    #32865
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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.

    #32882
    NicNic
    Participant

    Aha! 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.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.