-
Notifications
You must be signed in to change notification settings - Fork 50
Add Multisite Global Media support to Advanced Custom Fields image fields
Frank Bültge edited this page Mar 21, 2022
·
5 revisions
ACF Plugin Support With the help of the community was a solution integrated to support the ACF plugin with images from the global media. see #134
Update From 03/2022
Advanced Custom Fields (ACF) image fields support Multisite Global Media images in the admin, but not in the functions used to display them in the theme.
It's tricky for this plugin to handle wp_get_attachment_url
, because it requires a valid post ID.
So, in case it helps, here's a script to add Multisite Global Media support to Advanced Custom Fields image fields.
Drop it in your functions.php
or somewhere.
class Global_Media_ACF {
use \MultisiteGlobalMedia\Helper;
function __construct(){
$this->site = new MultisiteGlobalMedia\Site();
$this->siteSwitcher = new MultisiteGlobalMedia\SingleSwitcher();
$this->store = acf_get_store( 'values' );
add_filter( 'acf/load_value/type=image', [ $this, 'acf_load_value' ], 10, 3 );
}
// Fetch ACF file fields across sites when the global prefix is used.
// We hook into 'load_value' which usually runs just before 'format_value'.
// Then get the formatted output of the field in the global media site's context,
// and store it in ACF's cache. So when format_value tries to use this value,
// it will find the formatted one already in the cache.
// This works around acf_format_value requiring a valid att ID as input, but
// returning a string/array as output, so it can't be easily filtered.
function acf_load_value( $value, $post_id, $field ) {
if ( $this->idPrefixIncludedInAttachmentId( (int)$value, $this->site->idSitePrefix() ) ) {
$formatted = $this->stripSiteIdPrefixFromAttachmentId( $this->site->idSitePrefix(), $value );
$this->siteSwitcher->switchToBlog( $this->site->id() );
$formatted = acf_format_value( $formatted, $post_id, $field );
$this->siteSwitcher->restoreBlog();
$this->store->set( "$post_id:{$field['name']}:formatted", $formatted );
}
// This filter doesn't modify the loaded value. Return it as-is.
return $value;
}
}
new Global_Media_ACF();
source is issue #86