Skip to content

Instantly share code, notes, and snippets.

@BinaryMoon
Last active May 1, 2021 13:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryMoon/cd1eb239d4a14ba3ab45b756e4c64828 to your computer and use it in GitHub Desktop.
Save BinaryMoon/cd1eb239d4a14ba3ab45b756e4c64828 to your computer and use it in GitHub Desktop.
Make WordPress function Get_Post_Gallery work for Gutenberg powered sites.
<?php
/**
* Plugin Name: Get Post Gallery Polyfill
* Plugin URI: https://prothemedesign.com
* Description: Make Get_Post_Gallery work for Gutenberg powered sites.
* Author: Ben Gillbanks
* Version: 1.0
* Author URI: https://prothemedesign.com
*
* @package ptd
*/
/**
* A get_post_gallery() polyfill for Gutenberg
*
* @param string $gallery The current gallery html that may have already been found (through shortcodes).
* @param int $post The post id.
* @return string The gallery html.
*/
function bm_get_post_gallery( $gallery, $post ) {
// Already found a gallery so lets quit.
if ( $gallery ) {
return $gallery;
}
// Check the post exists.
$post = get_post( $post );
if ( ! $post ) {
return $gallery;
}
// Not using Gutenberg so let's quit.
if ( ! function_exists( 'has_blocks' ) ) {
return $gallery;
}
// Not using blocks so let's quit.
if ( ! has_blocks( $post->post_content ) ) {
return $gallery;
}
/**
* Search for gallery blocks and then, if found, return the html from the
* first gallery block.
*
* Thanks to Gabor for help with the regex:
* https://twitter.com/javorszky/status/1043785500564381696.
*/
$pattern = "/<!--\ wp:gallery.*-->([\s\S]*?)<!--\ \/wp:gallery -->/i";
preg_match_all( $pattern, $post->post_content, $the_galleries );
// Check a gallery was found and if so change the gallery html.
if ( ! empty( $the_galleries[1] ) ) {
$gallery = reset( $the_galleries[1] );
}
return $gallery;
}
add_filter( 'get_post_gallery', 'bm_get_post_gallery', 10, 2 );
@ulziibat-n
Copy link

Hello. How can i use this snippet?

@ulziibat-n
Copy link

Can i use it like this
`/**
* Retrieve the first gallery in the post
*/
$prefix_gallery = get_post_gallery( $post->ID, false );

		if ( $prefix_gallery ) {
			echo '<div class="swiper-container"><div class="swiper-wrapper">';
			if ( is_array( $prefix_gallery ) ) {
				foreach ( $prefix_gallery['src'] as $prefix_gallery_image_url ) {
					echo '<div class="swiper-slide"><img src="' . esc_url( $prefix_gallery_image_url ) . '"></div>';
				}
			}
			echo '</div></div>';
		} else {
			prefix_post_thumbnail();
		}`

@BinaryMoon
Copy link
Author

Yep - you use get_post_gallery as normal and this code will do its thing.

@kenpoc4
Copy link

kenpoc4 commented May 12, 2020

Hi, very good your plugin, but if you want to get the id of the gallery images, just return: string (1) "".
Is there any specific way to get the IDs?

@BinaryMoon
Copy link
Author

I'm afraid I don't have a solution for that. This grabs the html for the gallery, and doesn't get the gallery info. There's probably another regex you could use that would get that information but I don't know what it is.

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