: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
$args = apply_filters( 'get_attached_media_args', $args, $type, $post );
$children = get_children( $args );
* Filters the list of media attached to the given post.
* @param WP_Post[] $children Array of media attached to the given post.
* @param string $type Mime type of the media desired.
* @param WP_Post $post Post object.
return (array) apply_filters( 'get_attached_media', $children, $type, $post );
* Checks the HTML content for an audio, video, object, embed, or iframe tags.
* @param string $content A string of HTML which might contain media elements.
* @param string[] $types An array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'.
* @return string[] Array of found HTML media elements.
function get_media_embedded_in_content( $content, $types = null ) {
* Filters the embedded media types that are allowed to be returned from the content blob.
* @param string[] $allowed_media_types An array of allowed media types. Default media types are
* 'audio', 'video', 'object', 'embed', and 'iframe'.
$allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) );
if ( ! empty( $types ) ) {
if ( ! is_array( $types ) ) {
$types = array( $types );
$allowed_media_types = array_intersect( $allowed_media_types, $types );
$tags = implode( '|', $allowed_media_types );
if ( preg_match_all( '#<(?P<tag>' . $tags . ')[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#', $content, $matches ) ) {
foreach ( $matches[0] as $match ) {
* Retrieves galleries from the passed post's content.
* @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 ( ! has_shortcode( $post->post_content, 'gallery' ) && ! has_block( 'gallery', $post->post_content ) ) {
if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $shortcode ) {
if ( 'gallery' === $shortcode[2] ) {
$shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
// 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 );
preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER );
$galleries[] = array_merge(
'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'] ) {
// 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'] );
// 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>';
// 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 ) ) {
'ids' => implode( ',', $ids ),
// Old Gallery block format as HTML.
$galleries[] = $block['innerHTML'];
// 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.
foreach ( $ids as $id ) {
$url = wp_get_attachment_url( $id );
if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
'ids' => implode( ',', $ids ),
// 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 ) ) {
$galleries[] = array( 'src' => $srcs );
* Filters the list of all found galleries in the given post.
* @param array $galleries Associative array of all found post galleries.
* @param WP_Post $post Post object.
return apply_filters( 'get_post_galleries', $galleries, $post );
* Checks a specified post's content for gallery and, if present, return the first
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param bool $html Optional. Whether to return HTML or data. Default is true.
* @return string|array Gallery data and srcs parsed from the expanded shortcode.
function get_post_gallery( $post = 0, $html = true ) {
$galleries = get_post_galleries( $post, $html );
$gallery = reset( $galleries );
* Filters the first-found post gallery.
* @param array $gallery The first-found post gallery.
* @param int|WP_Post $post Post ID or object.
* @param array $galleries Associative array of all found post galleries.
return apply_filters( 'get_post_gallery', $gallery, $post, $galleries );
* Retrieves the image srcs from galleries from a post's content, if present.
* @see get_post_galleries()
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
* @return array A list of lists, each containing image srcs parsed.
* from an expanded shortcode
function get_post_galleries_images( $post = 0 ) {
$galleries = get_post_galleries( $post, false );
return wp_list_pluck( $galleries, 'src' );
* Checks a post's content for galleries and return the image srcs for the first found gallery.
* @see get_post_gallery()
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
* @return string[] A list of a gallery's image srcs in order.
function get_post_gallery_images( $post = 0 ) {
$gallery = get_post_gallery( $post, false );
return empty( $gallery['src'] ) ? array() : $gallery['src'];
* Maybe attempts to generate attachment metadata, if missing.
* @param WP_Post $attachment Attachment object.
function wp_maybe_generate_attachment_metadata( $attachment ) {
if ( empty( $attachment ) || empty( $attachment->ID ) ) {
$attachment_id = (int) $attachment->ID;
$file = get_attached_file( $attachment_id );
$meta = wp_get_attachment_metadata( $attachment_id );
if ( empty( $meta ) && file_exists( $file ) ) {
$_meta = get_post_meta( $attachment_id );
$_lock = 'wp_generating_att_' . $attachment_id;
if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) {
set_transient( $_lock, $file );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
delete_transient( $_lock );
* Tries to convert an attachment URL into a post ID.
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $url The URL to resolve.
* @return int The found post ID, or 0 on failure.
function attachment_url_to_postid( $url ) {
$dir = wp_get_upload_dir();
$site_url = parse_url( $dir['url'] );
$image_path = parse_url( $path );
// Force the protocols to match if needed.
if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) {
$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path );
if ( str_starts_with( $path, $dir['baseurl'] . '/' ) ) {
$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
$results = $wpdb->get_results( $sql );
// Use the first available result, but prefer a case-sensitive match, if exists.
$post_id = reset( $results )->post_id;
if ( count( $results ) > 1 ) {
foreach ( $results as $result ) {
if ( $path === $result->meta_value ) {
$post_id = $result->post_id;
* Filters an attachment ID found by URL.
* @param int|null $post_id The post_id (if any) found by the function.
* @param string $url The URL being looked up.
return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url );
* Returns the URLs for CSS files used in an iframe-sandbox'd TinyMCE media view.
* @return string[] The relevant CSS file URLs.
function wpview_media_sandbox_styles() {
$version = 'ver=' . get_bloginfo( 'version' );
$mediaelement = includes_url( "js/mediaelement/mediaelementplayer-legacy.min.css?$version" );
$wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" );
return array( $mediaelement, $wpmediaelement );
* Registers the personal data exporter for media.
* @param array[] $exporters An array of personal data exporters, keyed by their ID.
* @return array[] Updated array of personal data exporters.
function wp_register_media_personal_data_exporter( $exporters ) {
$exporters['wordpress-media'] = array(
'exporter_friendly_name' => __( 'WordPress Media' ),
'callback' => 'wp_media_personal_data_exporter',
* Finds and exports attachments associated with an email address.
* @param string $email_address The attachment owner email address.
* @param int $page Attachment page number.
* An array of personal data.
* @type array[] $data An array of personal data arrays.
* @type bool $done Whether the exporter is finished.
function wp_media_personal_data_exporter( $email_address, $page = 1 ) {
// Limit us to 50 attachments at a time to avoid timing out.
$data_to_export = array();
$user = get_user_by( 'email', $email_address );
'data' => $data_to_export,
$post_query = new WP_Query(
'posts_per_page' => $number,
'post_type' => 'attachment',
foreach ( (array) $post_query->posts as $post ) {
$attachment_url = wp_get_attachment_url( $post->ID );
$post_data_to_export = array(
'value' => $attachment_url,
$data_to_export[] = array(
'group_label' => __( 'Media' ),
'group_description' => __( 'User’s media data.' ),
'item_id' => "post-{$post->ID}",
'data' => $post_data_to_export,
$done = $post_query->max_num_pages <= $page;
'data' => $data_to_export,
* Adds additional default image sub-sizes.
* These sizes are meant to enhance the way WordPress displays images on the front-end on larger,
* high-density devices. They make it possible to generate more suitable `srcset` and `sizes` attributes
* when the users upload large images.
* The sizes can be changed or removed by themes and plugins but that is not recommended.
* The size "names" reflect the image dimensions, so changing the sizes would be quite misleading.
function _wp_add_additional_image_sizes() {
add_image_size( '1536x1536', 1536, 1536 );
add_image_size( '2048x2048', 2048, 2048 );
* Callback to enable showing of the user error when uploading .heic images.
* @param array[] $plupload_settings The settings for Plupload.js.
* @return array[] Modified settings for Plupload.js.
function wp_show_heic_upload_error( $plupload_settings ) {
$plupload_settings['heic_upload_error'] = true;
return $plupload_settings;
* Allows PHP's getimagesize() to be debuggable when necessary.
* @since 5.8.0 Added support for WebP images.
* @since 6.5.0 Added support for AVIF images.