get_post_galleries

Retrieves galleries from the passed post's content.

Description

(array) get_post_galleries( (int|WP_Post) $post, (bool) $html = true ); 

Returns (array)

A list of arrays, each containing gallery data and srcs parsed from the expanded shortcode.

Parameters (2)

0. $post (int|WP_Post)
Post ID or object.
1. $html — Optional. (bool) => true
Whether to return HTML or data in the array. Default true.

Usage

  1. if ( !function_exists( 'get_post_galleries' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Post ID or object. 
  5. $post = null; 
  6.  
  7. // Optional. Whether to return HTML or data in the array. Default true. 
  8. $html = true; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_post_galleries($post, $html); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/media.php  
  1. function get_post_galleries( $post, $html = true ) { 
  2. if ( ! $post = get_post( $post ) ) 
  3. return array(); 
  4.  
  5. if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 
  6. return array(); 
  7.  
  8. $galleries = array(); 
  9. if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) { 
  10. foreach ( $matches as $shortcode ) { 
  11. if ( 'gallery' === $shortcode[2] ) { 
  12. $srcs = array(); 
  13.  
  14. $gallery = do_shortcode_tag( $shortcode ); 
  15. if ( $html ) { 
  16. $galleries[] = $gallery; 
  17. } else { 
  18. preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER ); 
  19. if ( ! empty( $src ) ) { 
  20. foreach ( $src as $s ) 
  21. $srcs[] = $s[2]; 
  22.  
  23. $data = shortcode_parse_atts( $shortcode[3] ); 
  24. $data['src'] = array_values( array_unique( $srcs ) ); 
  25. $galleries[] = $data; 
  26.  
  27. /** 
  28. * Filters the list of all found galleries in the given post. 
  29. * @since 3.6.0 
  30. * @param array $galleries Associative array of all found post galleries. 
  31. * @param WP_Post $post Post object. 
  32. */ 
  33. return apply_filters( 'get_post_galleries', $galleries, $post );