Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems attaching to ACF file field from the "Global Media" Tab #98

Open
mikem33 opened this issue Nov 19, 2019 · 4 comments
Open

Problems attaching to ACF file field from the "Global Media" Tab #98

mikem33 opened this issue Nov 19, 2019 · 4 comments

Comments

@mikem33
Copy link

mikem33 commented Nov 19, 2019

I have some ACF File fields that if I attach to them PDF's files uploaded in the main site from the "Global Media" tab, in first instance it get attached to them without any apparent problem but when I reload the page after saving it, the file is no longer attached to them.

If the file is from the current site it works without problem. I suppose this is Gutenberg related.
Any idea? Thank you in advance!

@feldmarv
Copy link

We are faceing the same isse on our current installation.

WP Version 5.0.3
Multisite Global Media 0.1.0-dev-2
Advanced Custom Fields PRO (ACF) Version 5.8.1

@Leobar00
Copy link

Same issue of @mikem33 with gallery ACF

@bueltge
Copy link
Owner

bueltge commented Jun 23, 2022

@feldmarv @mikem33 and @Leobar00 have you tried this with the last version, that have several enhancements for the ACF plugin?

@samykantari
Copy link

@bueltge @feldmarv @mikem33
I have the same problem, with :
WP Version 6.0.1
Multisite Global Media 0.2.0
Advanced Custom Fields PRO (ACF) Version 5.12.3

Searching in the ACF source code, I found a solution

In the file (acf) : acf/includes/api/api-helpers.php function acf_get_attachment

At first :

// Allow filter to short-circuit load attachment logic.
// Alternatively, this filter may be used to switch blogs for multisite media functionality.
$response = apply_filters( 'acf/pre_load_attachment', null, $attachment );
if ( $response !== null ) {
	return $response;
}

So we have an interesting hook "acf/pre_load_attachment" and the answer expected by ACF is an array

We can use the hook, if we are in the context "global media" then we can make the "switch" get the info from the media and return it

add_action(
	'acf/init',
	function () {
		add_filter(
			'acf/pre_load_attachment',
			function ( $value, $attachment ) {
				$siteIdPrefix = '100000';

				if ( false !== strpos( (string) $attachment, $siteIdPrefix ) ) {
					$formatted = str_replace( $siteIdPrefix, '', $attachment );
					switch_to_blog( get_main_site_id() );

					$fileNotExist = false;

					$attachment = get_post( $formatted );


					if ( ! $attachment ) {
						$fileNotExist = true;
					}
					if ( 'attachment' !== $attachment->post_type ) {
						$fileNotExist = true;
					}
					$response = false;

					if ( false === $fileNotExist ) {

						$meta          = wp_get_attachment_metadata( $attachment->ID );
						$attached_file = get_attached_file( $attachment->ID );
						if ( strpos( $attachment->post_mime_type, '/' ) !== false ) {
							list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
						} else {
							list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
						}

						$response = array(
							'ID'          => $attachment->ID,
							'id'          => $attachment->ID,
							'title'       => $attachment->post_title,
							'filename'    => wp_basename( $attached_file ),
							'filesize'    => 0,
							'url'         => wp_get_attachment_url( $attachment->ID ),
							'link'        => get_attachment_link( $attachment->ID ),
							'alt'         => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
							'author'      => $attachment->post_author,
							'description' => $attachment->post_content,
							'caption'     => $attachment->post_excerpt,
							'name'        => $attachment->post_name,
							'status'      => $attachment->post_status,
							'uploaded_to' => $attachment->post_parent,
							'date'        => $attachment->post_date_gmt,
							'modified'    => $attachment->post_modified_gmt,
							'menu_order'  => $attachment->menu_order,
							'mime_type'   => $attachment->post_mime_type,
							'type'        => $type,
							'subtype'     => $subtype,
							'icon'        => wp_mime_type_icon( $attachment->ID ),
						);

						if ( isset( $meta['filesize'] ) ) {
							$response['filesize'] = $meta['filesize'];
						} elseif ( file_exists( $attached_file ) ) {
							$response['filesize'] = filesize( $attached_file );
						}
					}


					restore_current_blog();

					return $response;
				}


				return $value;
			},
			10,
			2
		);
	}
);

@Leobar00 I don't have a fix for your problem But you can cheat :)
Create a gallery with images from the site (not "global media") and then go to each image and replace the media with a "global media" image
The problem is that if you change the gallery by adding an image you will have to replace everything again. So when creating the gallery, choose the number of images carefully :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants