$ show humanmade/network-media-library
The Network Media Library plugin for WordPress Multisite provides a central media library shared across all sites in the network. It achieves this by designating one site as the central media library and temporarily switching the context to that site during media operations (upload, insertion, editing, etc.). The plugin hooks into numerous WordPress actions and filters to intercept media-related requests and redirect them to the central site. It also includes compatibility layers for popular third-party plugins like Advanced Custom Fields and MultilingualPress to ensure they work correctly with the shared media library. The code is organized into a single main file with several classes encapsulating specific functionalities, such as managing context switches, preserving post thumbnail IDs during save operations, and handling ACF field rendering.
The LLM identified 2 files worth highlighting for reuse:
The ACF_Field_Rendering class manages the state of switching to the central media site during the rendering of ACF file-type fields. It ensures that after rendering such a field, the context is switched back to the original site. This pattern encapsulates the complexity of managing temporary state across multiple WordPress actions and is reusable in other contexts requiring similar state management.
The Post_Thumbnail_Saver class solves the problem of WordPress deleting the thumbnail ID during post insertion if it doesn't validate. It temporarily stores the thumbnail ID during the wp_insert_post_data filter and restores it after the post is saved via the save_post action. This pattern of using two hooks to capture and later restore data is a valuable technique for preserving data through the WordPress post save process and can be adapted for other similar use cases.