: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
// Remove intermediate and backup images if there are any.
if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
foreach ( $meta['sizes'] as $size => $sizeinfo ) {
$intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
if ( ! empty( $intermediate_file ) ) {
$intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
if ( ! empty( $meta['original_image'] ) ) {
if ( empty( $intermediate_dir ) ) {
$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
$original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file );
if ( ! empty( $original_image ) ) {
$original_image = path_join( $uploadpath['basedir'], $original_image );
if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) {
if ( is_array( $backup_sizes ) ) {
$del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
foreach ( $backup_sizes as $size ) {
$del_file = path_join( dirname( $meta['file'] ), $size['file'] );
if ( ! empty( $del_file ) ) {
$del_file = path_join( $uploadpath['basedir'], $del_file );
if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
* Retrieves attachment metadata for attachment ID.
* @since 6.0.0 The `$filesize` value was added to the returned array.
* @param int $attachment_id Attachment post ID. Defaults to global $post.
* @param bool $unfiltered Optional. If true, filters are not run. Default false.
* Attachment metadata. False on failure.
* @type int $width The width of the attachment.
* @type int $height The height of the attachment.
* @type string $file The file path relative to `wp-content/uploads`.
* @type array $sizes Keys are size slugs, each value is an array containing
* 'file', 'width', 'height', and 'mime-type'.
* @type array $image_meta Image metadata.
* @type int $filesize File size of the attachment.
function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
$attachment_id = (int) $attachment_id;
if ( ! $attachment_id ) {
$attachment_id = $post->ID;
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
* Filters the attachment meta data.
* @param array $data Array of meta data for the given attachment.
* @param int $attachment_id Attachment post ID.
return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id );
* Updates metadata for an attachment.
* @param int $attachment_id Attachment post ID.
* @param array $data Attachment meta data.
* @return int|false False if $post is invalid.
function wp_update_attachment_metadata( $attachment_id, $data ) {
$attachment_id = (int) $attachment_id;
$post = get_post( $attachment_id );
* Filters the updated attachment meta data.
* @param array $data Array of updated attachment meta data.
* @param int $attachment_id Attachment post ID.
$data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
return update_post_meta( $post->ID, '_wp_attachment_metadata', $data );
return delete_post_meta( $post->ID, '_wp_attachment_metadata' );
* Retrieves the URL for an attachment.
* @global string $pagenow The filename of the current screen.
* @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
* @return string|false Attachment URL, otherwise false.
function wp_get_attachment_url( $attachment_id = 0 ) {
$attachment_id = (int) $attachment_id;
$post = get_post( $attachment_id );
if ( 'attachment' !== $post->post_type ) {
$file = get_post_meta( $post->ID, '_wp_attached_file', true );
$uploads = wp_get_upload_dir();
if ( $uploads && false === $uploads['error'] ) {
// Check that the upload base exists in the file location.
if ( str_starts_with( $file, $uploads['basedir'] ) ) {
// Replace file location with url location.
$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file );
} elseif ( str_contains( $file, 'wp-content/uploads' ) ) {
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file );
// It's a newly-uploaded file, therefore $file is relative to the basedir.
$url = $uploads['baseurl'] . "/$file";
* If any of the above options failed, Fallback on the GUID as used pre-2.7,
* not recommended to rely upon this.
$url = get_the_guid( $post->ID );
// On SSL front end, URLs should be HTTPS.
if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
$url = set_url_scheme( $url );
* Filters the attachment URL.
* @param string $url URL for the given attachment.
* @param int $attachment_id Attachment post ID.
$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
* Retrieves the caption for an attachment.
* @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
* @return string|false Attachment caption on success, false on failure.
function wp_get_attachment_caption( $post_id = 0 ) {
$post_id = (int) $post_id;
$post = get_post( $post_id );
if ( 'attachment' !== $post->post_type ) {
$caption = $post->post_excerpt;
* Filters the attachment caption.
* @param string $caption Caption for the given attachment.
* @param int $post_id Attachment ID.
return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
* Retrieves URL for an attachment thumbnail.
* @since 6.1.0 Changed to use wp_get_attachment_image_url().
* @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
* @return string|false Thumbnail URL on success, false on failure.
function wp_get_attachment_thumb_url( $post_id = 0 ) {
$post_id = (int) $post_id;
* This uses image_downsize() which also looks for the (very) old format $image_meta['thumb']
* when the newer format $image_meta['sizes']['thumbnail'] doesn't exist.
$thumbnail_url = wp_get_attachment_image_url( $post_id, 'thumbnail' );
if ( empty( $thumbnail_url ) ) {
* Filters the attachment thumbnail URL.
* @param string $thumbnail_url URL for the attachment thumbnail.
* @param int $post_id Attachment ID.
return apply_filters( 'wp_get_attachment_thumb_url', $thumbnail_url, $post_id );
* Verifies an attachment is of a given type.
* @param string $type Attachment type. Accepts `image`, `audio`, `video`, or a file extension.
* @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
* @return bool True if an accepted type or a matching file extension, false otherwise.
function wp_attachment_is( $type, $post = null ) {
$post = get_post( $post );
$file = get_attached_file( $post->ID );
if ( str_starts_with( $post->post_mime_type, $type . '/' ) ) {
$check = wp_check_filetype( $file );
if ( empty( $check['ext'] ) ) {
if ( 'import' !== $post->post_mime_type ) {
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif' );
return in_array( $ext, $image_exts, true );
return in_array( $ext, wp_get_audio_extensions(), true );
return in_array( $ext, wp_get_video_extensions(), true );
* Determines whether an attachment is an image.
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
* @since 4.2.0 Modified into wrapper for wp_attachment_is() and
* allowed WP_Post object to be passed.
* @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
* @return bool Whether the attachment is an image.
function wp_attachment_is_image( $post = null ) {
return wp_attachment_is( 'image', $post );
* Retrieves the icon for a MIME type or attachment.
* @since 6.5.0 Added the `$preferred_ext` parameter.
* @param string|int $mime MIME type or attachment ID.
* @param string $preferred_ext File format to prefer in return. Default '.png'.
* @return string|false Icon, false otherwise.
function wp_mime_type_icon( $mime = 0, $preferred_ext = '.png' ) {
if ( ! is_numeric( $mime ) ) {
$icon = wp_cache_get( "mime_type_icon_$mime" );
// Check if preferred file format variable is present and is a validly formatted file extension.
if ( ! empty( $preferred_ext ) && is_string( $preferred_ext ) && ! str_starts_with( $preferred_ext, '.' ) ) {
$preferred_ext = '.' . strtolower( $preferred_ext );
if ( is_numeric( $mime ) ) {
$post = get_post( $mime );
$post_id = (int) $post->ID;
$file = get_attached_file( $post_id );
$ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $file );
$ext_type = wp_ext2type( $ext );
$post_mimes[] = $ext_type;
$mime = $post->post_mime_type;
$icon_files = wp_cache_get( 'icon_files' );
if ( ! is_array( $icon_files ) ) {
* Filters the icon directory path.
* @param string $path Icon directory absolute path.
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
* Filters the icon directory URI.
* @param string $uri Icon directory URI.
$icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) );
* Filters the array of icon directory URIs.
* @param string[] $uris Array of icon directory URIs keyed by directory absolute path.
$dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) );
$keys = array_keys( $dirs );
$dir = array_shift( $keys );
$uri = array_shift( $dirs );
while ( false !== $file = readdir( $dh ) ) {
$file = wp_basename( $file );
if ( str_starts_with( $file, '.' ) ) {
$ext = strtolower( substr( $file, -4 ) );
if ( ! in_array( $ext, array( '.svg', '.png', '.gif', '.jpg' ), true ) ) {
if ( is_dir( "$dir/$file" ) ) {
$dirs[ "$dir/$file" ] = "$uri/$file";
$all_icons[ "$dir/$file" ] = "$uri/$file";
if ( $ext === $preferred_ext ) {
$icon_files[ "$dir/$file" ] = "$uri/$file";
// If directory only contained icons of a non-preferred format, return those.
if ( empty( $icon_files ) ) {
$icon_files = $all_icons;
wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
// Icon wp_basename - extension = MIME wildcard.
foreach ( $icon_files as $file => $uri ) {
$types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ];
if ( ! empty( $mime ) ) {
$post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) );
$post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 );
$post_mimes[] = str_replace( '/', '_', $mime );
$matches = wp_match_mime_types( array_keys( $types ), $post_mimes );
$matches['default'] = array( 'default' );
foreach ( $matches as $match => $wilds ) {
foreach ( $wilds as $wild ) {
if ( ! isset( $types[ $wild ] ) ) {
if ( ! is_numeric( $mime ) ) {
wp_cache_add( "mime_type_icon_$mime", $icon );