network-media-library icon indicating copy to clipboard operation
network-media-library copied to clipboard

Support `wp_get_attachment_url()`

Open johnbillion opened this issue 5 years ago • 1 comments

wp_get_attachment_url() should work transparently in the same way that wp_get_attachment_image_src() does.

This needs some filters adding to WordPress core in order for it to be able to work.

johnbillion avatar Aug 26 '18 22:08 johnbillion

There is a wp_get_attachment_url filter which could be used for this.

	/**
	 * Filters the attachment URL.
	 *
	 * @since 2.1.0
	 *
	 * @param string $url           URL for the given attachment.
	 * @param int    $attachment_id Attachment post ID.
	 */
	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );

Probably this would fix:

add_filter( 'wp_get_attachment_url', function( $image, $attachment_id ) {
	static $switched = false;

	if ( $switched ) {
		return $image;
	}

	if ( is_media_site() ) {
		return $image;
	}

	switch_to_media_site();

	$switched = true;
	$image    = wp_get_attachment_url( $attachment_id );
	$switched = false;

	restore_current_blog();

	return $image;
}, 999, 2 );

nextend avatar May 15 '20 09:05 nextend