get_post_galleries_images()WP 3.6.0

Retrieve the image srcs from galleries from a post's content, if present

1 time — 0.009545 sec (very slow) | 50000 times — 595.55 sec (extremely slow) | PHP 7.0.4, WP 4.4.2

No Hooks.

Return

Array. A list of lists, each containing image srcs parsed. from an expanded shortcode

Usage

get_post_galleries_images( $post );
$post(int|WP_Post)
Post ID or WP_Post object.
Default: global $post

Examples

0

#1 Get a list of all image's src from all galleries of post 2179

Let's say there are 2 galleries in the post 2179 - 2 shortcodes [gallery] and we need to get all the links of these galleries' pictures. In the shortcode we specify the IDs of these images attachments:

$gal = get_post_galleries_images( 2179 );

/* $gal will contain
Array
(
	[0] => Array
		(
			[0] => http://wp-kama.com/wp-content/uploads/2016/02/image12-80x80.png
			[1] => http://wp-kama.com/wp-content/uploads/2016/02/image11-80x80.png
			[2] => http://wp-kama.com/wp-content/uploads/2016/02/image10-80x80.png
		)

	[1] => Array
		(
			[0] => http://wp-kama.com/wp-content/uploads/2016/02/image008-80x80.jpg
			[1] => http://wp-kama.com/wp-content/uploads/2016/02/image007-80x80.jpg
			[2] => http://wp-kama.com/wp-content/uploads/2016/02/image006-80x80.jpg
		)

)
*/
0

#2 Append the image URLs to the post content if post has gallery in content

A simple example of how to append the raw image URLs to the content of any post or page that has at least one gallery.

add_filter( 'the_content', 'wpdocs_show_gallery_image_urls' );

/**
 * Add list of image URLs to the content if displaying 
 * a post with one or more image galleries.
 *
 * @param string $content Post content.
 *
 * @return string (Maybe modified) post content.
 */
function wpdocs_show_gallery_image_urls( $content ) {
	global $post;

	// Only do this on singular items.
	if ( ! is_singular() ) {
		return $content;
	}

	// Make sure the post has a gallery in it.
	if ( ! has_shortcode( $post->post_content, 'gallery' ) ) {
		return $content;
	}

	// Retrieve all galleries of this post.
	$galleries = get_post_galleries_images( $post );

	if ( ! empty( $galleries ) ) {
		$image_list = '<ul>';

		// Loop through all galleries found
		foreach( $galleries as $gallery ) {
			// Loop through each image in each gallery.
			foreach ( $gallery as $image ) {
				$image_list .= '<li>' . $image . '</li>';
			}
		}

		$image_list .= '</ul>';

		// Append our image list to the content of our post
		$content .= $image_list;
	}

	return $content;
}

Notes

Changelog

Since 3.6.0 Introduced.

get_post_galleries_images() code WP 6.5.2

function get_post_galleries_images( $post = 0 ) {
	$galleries = get_post_galleries( $post, false );
	return wp_list_pluck( $galleries, 'src' );
}