get_post_galleries

The timeline below displays how wordpress function get_post_galleries has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 9.3

/**
 * Retrieves galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Post ID or object.
 * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *               from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    $post = get_post($post);
    if (!$post) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery') && !has_block('gallery', $post->post_content)) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $shortcode_attrs = shortcode_parse_atts($shortcode[3]);
                if (!is_array($shortcode_attrs)) {
                    $shortcode_attrs = array();
                }
                // Specify the post ID of the gallery we're viewing if the shortcode doesn't reference another post already.
                if (!isset($shortcode_attrs['id'])) {
                    $shortcode[3] .= ' id="' . (int) $post->ID . '"';
                }
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $galleries[] = array_merge($shortcode_attrs, array('src' => array_values(array_unique($srcs))));
                }
            }
        }
    }
    if (has_block('gallery', $post->post_content)) {
        $post_blocks = parse_blocks($post->post_content);
        while ($block = array_shift($post_blocks)) {
            $has_inner_blocks = !empty($block['innerBlocks']);
            // Skip blocks with no blockName and no innerHTML.
            if (!$block['blockName']) {
                continue;
            }
            // Skip non-Gallery blocks.
            if ('core/gallery' !== $block['blockName']) {
                // Move inner blocks into the root array before skipping.
                if ($has_inner_blocks) {
                    array_push($post_blocks, ...$block['innerBlocks']);
                }
                continue;
            }
            // New Gallery block format as HTML.
            if ($has_inner_blocks && $html) {
                $block_html = wp_list_pluck($block['innerBlocks'], 'innerHTML');
                $galleries[] = '<figure>' . implode(' ', $block_html) . '</figure>';
                continue;
            }
            $srcs = array();
            // New Gallery block format as an array.
            if ($has_inner_blocks) {
                $attrs = wp_list_pluck($block['innerBlocks'], 'attrs');
                $ids = wp_list_pluck($attrs, 'id');
                foreach ($ids as $id) {
                    $url = wp_get_attachment_url($id);
                    if (is_string($url) && !in_array($url, $srcs, true)) {
                        $srcs[] = $url;
                    }
                }
                $galleries[] = array('ids' => implode(',', $ids), 'src' => $srcs);
                continue;
            }
            // Old Gallery block format as HTML.
            if ($html) {
                $galleries[] = $block['innerHTML'];
                continue;
            }
            // Old Gallery block format as an array.
            $ids = (!empty($block['attrs']['ids'])) ? $block['attrs']['ids'] : array();
            // If present, use the image IDs from the JSON blob as canonical.
            if (!empty($ids)) {
                foreach ($ids as $id) {
                    $url = wp_get_attachment_url($id);
                    if (is_string($url) && !in_array($url, $srcs, true)) {
                        $srcs[] = $url;
                    }
                }
                $galleries[] = array('ids' => implode(',', $ids), 'src' => $srcs);
                continue;
            }
            // Otherwise, extract srcs from the innerHTML.
            preg_match_all('#src=([\'"])(.+?)\1#is', $block['innerHTML'], $found_srcs, PREG_SET_ORDER);
            if (!empty($found_srcs[0])) {
                foreach ($found_srcs as $src) {
                    if (isset($src[2]) && !in_array($src[2], $srcs, true)) {
                        $srcs[] = $src[2];
                    }
                }
            }
            $galleries[] = array('src' => $srcs);
        }
    }
    /**
     * Filters the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 5.9

/**
 * Retrieves galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Post ID or object.
 * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *               from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    $post = get_post($post);
    if (!$post) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery') && !has_block('gallery', $post->post_content)) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $shortcode_attrs = shortcode_parse_atts($shortcode[3]);
                if (!is_array($shortcode_attrs)) {
                    $shortcode_attrs = array();
                }
                // Specify the post ID of the gallery we're viewing if the shortcode doesn't reference another post already.
                if (!isset($shortcode_attrs['id'])) {
                    $shortcode[3] .= ' id="' . (int) $post->ID . '"';
                }
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $galleries[] = array_merge($shortcode_attrs, array('src' => array_values(array_unique($srcs))));
                }
            }
        }
    }
    if (has_block('gallery', $post->post_content)) {
        $post_blocks = parse_blocks($post->post_content);
        while ($block = array_shift($post_blocks)) {
            $has_inner_blocks = !empty($block['innerBlocks']);
            // Skip blocks with no blockName and no innerHTML.
            if (!$block['blockName']) {
                continue;
            }
            // All blocks nested inside non-Gallery blocks should be in the root array.
            if ($has_inner_blocks && 'core/gallery' !== $block['blockName']) {
                array_push($post_blocks, ...$block['innerBlocks']);
                continue;
            }
            // New Gallery block format as HTML.
            if ($has_inner_blocks && $html) {
                $block_html = wp_list_pluck($block['innerBlocks'], 'innerHTML');
                $galleries[] = '<figure>' . implode(' ', $block_html) . '</figure>';
                continue;
            }
            $srcs = array();
            // New Gallery block format as an array.
            if ($has_inner_blocks) {
                $attrs = wp_list_pluck($block['innerBlocks'], 'attrs');
                $ids = wp_list_pluck($attrs, 'id');
                foreach ($ids as $id) {
                    $url = wp_get_attachment_url($id);
                    if (is_string($url) && !in_array($url, $srcs, true)) {
                        $srcs[] = $url;
                    }
                }
                $galleries[] = array('ids' => implode(',', $ids), 'src' => $srcs);
                continue;
            }
            // Old Gallery block format as HTML.
            if ($html) {
                $galleries[] = $block['innerHTML'];
                continue;
            }
            // Old Gallery block format as an array.
            $ids = (!empty($block['attrs']['ids'])) ? $block['attrs']['ids'] : array();
            // If present, use the image IDs from the JSON blob as canonical.
            if (!empty($ids)) {
                foreach ($ids as $id) {
                    $url = wp_get_attachment_url($id);
                    if (is_string($url) && !in_array($url, $srcs, true)) {
                        $srcs[] = $url;
                    }
                }
                $galleries[] = array('ids' => implode(',', $ids), 'src' => $srcs);
                continue;
            }
            // Otherwise, extract srcs from the innerHTML.
            preg_match_all('#src=([\'"])(.+?)\1#is', $block['innerHTML'], $found_srcs, PREG_SET_ORDER);
            if (!empty($found_srcs[0])) {
                foreach ($found_srcs as $src) {
                    if (isset($src[2]) && !in_array($src[2], $srcs, true)) {
                        $srcs[] = $src[2];
                    }
                }
            }
            $galleries[] = array('src' => $srcs);
        }
    }
    /**
     * Filters the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 5.6

/**
 * Retrieves galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Post ID or object.
 * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *               from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    $post = get_post($post);
    if (!$post) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $shortcode_attrs = shortcode_parse_atts($shortcode[3]);
                if (!is_array($shortcode_attrs)) {
                    $shortcode_attrs = array();
                }
                // Specify the post ID of the gallery we're viewing if the shortcode doesn't reference another post already.
                if (!isset($shortcode_attrs['id'])) {
                    $shortcode[3] .= ' id="' . (int) $post->ID . '"';
                }
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $galleries[] = array_merge($shortcode_attrs, array('src' => array_values(array_unique($srcs))));
                }
            }
        }
    }
    /**
     * Filters the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 5.5

/**
 * Retrieves galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Post ID or object.
 * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *               from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    $post = get_post($post);
    if (!$post) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $shortcode_attrs = shortcode_parse_atts($shortcode[3]);
                if (!is_array($shortcode_attrs)) {
                    $shortcode_attrs = array();
                }
                // Specify the post ID of the gallery we're viewing if the shortcode doesn't reference another post already.
                if (!isset($shortcode_attrs['id'])) {
                    $shortcode[3] .= ' id="' . intval($post->ID) . '"';
                }
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $galleries[] = array_merge($shortcode_attrs, array('src' => array_values(array_unique($srcs))));
                }
            }
        }
    }
    /**
     * Filters the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 5.3

/**
 * Retrieves galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Post ID or object.
 * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *               from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    $post = get_post($post);
    if (!$post) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $shortcode_attrs = shortcode_parse_atts($shortcode[3]);
                if (!is_array($shortcode_attrs)) {
                    $shortcode_attrs = array();
                }
                // Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already.
                if (!isset($shortcode_attrs['id'])) {
                    $shortcode[3] .= ' id="' . intval($post->ID) . '"';
                }
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $galleries[] = array_merge($shortcode_attrs, array('src' => array_values(array_unique($srcs))));
                }
            }
        }
    }
    /**
     * Filters the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 7.3

/**
 * Retrieves galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Post ID or object.
 * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *               from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    if (!$post = get_post($post)) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $shortcode_attrs = shortcode_parse_atts($shortcode[3]);
                if (!is_array($shortcode_attrs)) {
                    $shortcode_attrs = array();
                }
                // Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already.
                if (!isset($shortcode_attrs['id'])) {
                    $shortcode[3] .= ' id="' . intval($post->ID) . '"';
                }
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $galleries[] = array_merge($shortcode_attrs, array('src' => array_values(array_unique($srcs))));
                }
            }
        }
    }
    /**
     * Filters the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 4.6

/**
 * Retrieves galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Post ID or object.
 * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *               from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    if (!$post = get_post($post)) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $data = shortcode_parse_atts($shortcode[3]);
                    $data['src'] = array_values(array_unique($srcs));
                    $galleries[] = $data;
                }
            }
        }
    }
    /**
     * Filters the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 4.2

/**
 * Retrieves galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Post ID or object.
 * @param bool        $html Optional. Whether to return HTML or data in the array. Default true.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *               from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    if (!$post = get_post($post)) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $data = shortcode_parse_atts($shortcode[3]);
                    $data['src'] = array_values(array_unique($srcs));
                    $galleries[] = $data;
                }
            }
        }
    }
    /**
     * Filter the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 4.0

/**
 * Retrieve galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Optional. Post ID or object.
 * @param bool        $html Whether to return HTML or data in the array.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *		         from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    if (!$post = get_post($post)) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $data = shortcode_parse_atts($shortcode[3]);
                    $data['src'] = array_values(array_unique($srcs));
                    $galleries[] = $data;
                }
            }
        }
    }
    /**
     * Filter the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 3.9

/**
 * Retrieve galleries from the passed post's content.
 *
 * @since 3.6.0
 *
 * @param int|WP_Post $post Optional. Post ID or object.
 * @param bool        $html Whether to return HTML or data in the array.
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *		         from the expanded shortcode.
 */
function get_post_galleries($post, $html = true)
{
    if (!$post = get_post($post)) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $count = 1;
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $data = shortcode_parse_atts($shortcode[3]);
                    $data['src'] = array_values(array_unique($srcs));
                    $galleries[] = $data;
                }
            }
        }
    }
    /**
     * Filter the list of all found galleries in the given post.
     *
     * @since 3.6.0
     *
     * @param array   $galleries Associative array of all found post galleries.
     * @param WP_Post $post      Post object.
     */
    return apply_filters('get_post_galleries', $galleries, $post);
}

WordPress Version: 3.7

/**
 * Retrieve galleries from the passed post's content
 *
 * @since 3.6.0
 *
 * @param mixed $post Optional. Post ID or object.
 * @param boolean $html Whether to return HTML or data in the array
 * @return array A list of arrays, each containing gallery data and srcs parsed
 *		from the expanded shortcode
 */
function get_post_galleries($post, $html = true)
{
    if (!$post = get_post($post)) {
        return array();
    }
    if (!has_shortcode($post->post_content, 'gallery')) {
        return array();
    }
    $galleries = array();
    if (preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $shortcode) {
            if ('gallery' === $shortcode[2]) {
                $srcs = array();
                $count = 1;
                $gallery = do_shortcode_tag($shortcode);
                if ($html) {
                    $galleries[] = $gallery;
                } else {
                    preg_match_all('#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER);
                    if (!empty($src)) {
                        foreach ($src as $s) {
                            $srcs[] = $s[2];
                        }
                    }
                    $data = shortcode_parse_atts($shortcode[3]);
                    $data['src'] = array_values(array_unique($srcs));
                    $galleries[] = $data;
                }
            }
        }
    }
    return apply_filters('get_post_galleries', $galleries, $post);
}