Network Media Library: Fixing broken thumbnails

If you use the Network Media Library plugin, you might notice that your featured images are not carried over to the child blogs. This is due to the nonstandard way the plugin handles images: WordPress expects featured images (thumbnails) to exist on the same blog.

When a featured image ID is detected, Broadcast will try to save the image information for later use – but the image doesn’t exist on the blog, so the featured image is ignored.

This snippet will save the featured image ID anyways, and then set it to the same value on each child blog.


/**
 @brief Remember the thumbnail ID for later use.
 @since 2020-04-08 22:38:38
**/
function bc_threewp_broadcast_broadcasting_setup( $action )
{
 $bcd = $action->broadcasting_data;

 $post_id = $bcd->post->ID;
 $id = get_post_meta( $post_id, '_thumbnail_id', true );

 if ( $id < 1 )
 return;

 ThreeWP_Broadcast()->debug( 'Saving thumbnail %s', $id );
 $bcd->_thumbnail_id_saved = $id;
}
add_action( 'threewp_broadcast_broadcasting_setup', 'bc_threewp_broadcast_broadcasting_setup' );

/**
 @brief Set the child post's thumbnail ID to the same as the parent post's.
 @since 2020-04-08 22:38:38
**/
function bc_threewp_broadcast_broadcasting_before_restore_current_blog( $action )
{
 $bcd = $action->broadcasting_data;

 if ( ! isset( $bcd->_thumbnail_id_saved ) )
  return;

 $id = $bcd->_thumbnail_id_saved;
 ThreeWP_Broadcast()->debug( 'Setting thumbnail to %s', $id );
 update_post_meta( $bcd->new_post( 'ID' ), '_thumbnail_id', $id );
}
add_action( 'threewp_broadcast_broadcasting_before_restore_current_blog', 'bc_threewp_broadcast_broadcasting_before_restore_current_blog' );