In many ways, managing media within a multisite network is very similar to a traditional WordPress install.
When files are uploaded to a site within a network, the files are saved as an “attachment” post type within the associated wp_posts table for that install. For example, if the blog id for the site you’re uploading to is “2” then the attachment would save to the wp_2_posts table within your database. Any additional information for that file would also save to the wp_2_postmeta table.
Also, just like a traditional WordPress install, the files themselves will live on your server within the wp-content > uploads folder. The only difference with multisite is that there is an additional folder layers to sort the images based on which site within the network they were uploaded to. Continuing with the example above, that file would be uploaded to wp-content > uploads > sites > 2.
Accessing files throughout the network
If you’re wanting to access files uploaded to one site from another site within the network, there aren’t any direct or built-in ways of handling this. By this, I mean there aren’t multisite versions of the traditional “wp_get_attachment_image” or similar media functions.
However, we can use any of these functions as they are to access the media files from any blog within the network.
To accomplish this, we can take advantage of the switch_to_blog() and restore_current_blog() functions. These functions will allow you to act, while you’re working within one blog, as if you’re within another.
Here is an example of this that was used in a recent project:
For this example, we have a main site that aggregates the featured image, blog title, and link for all the blogs within the network for a grid on the homepage of the site. We loop through all the blogs assigning the information to the “$as_blogs” array. At the point in the snippet, you can see we’re switching to the different blogs and are able to use the traditional media function for the single install.
This approach allows you to access the files within your templates. To access the files within your dashboard, check out Network Shared Media plugin. This allows you to see the files from across your network via the Media upload box within your dashboard.
Have any other interesting uses for media in multisite? Hit me up in the comments 🙂
Leave a Reply