Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sync Multisite Avatars & Covers Upload Path
<?php
/**
* Sync Multisite Avatars & Covers Upload Path
* Ps: Multiblog must have been set for it to work.
* eg: define( 'BP_ENABLE_MULTIBLOG', true );
*/
function yzc_set_multiblog_shared_upload_dir() {
if ( ! bp_is_multiblog_mode() ) {
return;
}
$current_site = get_current_site();
if ( (int) bp_get_root_blog_id() !== (int) $current_site->blog_id ) {
$switched = true;
switch_to_blog( $current_site->blog_id );
}
buddypress()->upload_dir = wp_upload_dir();
if ( ! empty( $switched ) ) {
restore_current_blog();
}
}
add_action( 'bp_init', 'yzc_set_multiblog_shared_upload_dir', 1 );
/**
* Shared Media Library
*/
add_filter( 'wp_get_attachment_image_src', function( $image, $attachment_id, $size, bool $icon ) {
static $switched = false;
if ( $switched ) {
return $image;
}
foreach ( get_sites() as $b ) {
switch_to_blog( $b->blog_id );
$switched = true;
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
if ( ! empty( $image)) {
$switched = false;
restore_current_blog();
break;
}
restore_current_blog();
}
return $image;
}, 999, 4 );
/**
* Shared Media Library
*/
add_filter( 'youzify_activity_file_download_url', function( $url, $attachment_id ) {
if ( ! empty( $url ) ) {
return $url;
}
foreach ( get_sites() as $b ) {
switch_to_blog( $b->blog_id );
$url = wp_get_attachment_url( $attachment_id );
if ( ! empty( $url ) ) {
restore_current_blog();
return $url;
}
restore_current_blog();
}
return $url;
}, 999, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment