Why wp_get_attachment_image_src() Returns a URL to a Scaled Image That Isn’t on Disk

The symptom: a URL that resolves to nothing

After a migration, a featured image or inline block image renders as a broken image. The browser requests a URL ending in -scaled.jpg and gets a 404. You open the Media Library, and the attachment looks fine: the thumbnail renders, the dimensions are listed, the attachment page loads. You check the uploads directory over SSH and the original file is there. The scaled copy is not.

The instinct is to blame wp_get_attachment_image_src(). It is not the culprit. The function is doing exactly what its source says it does: reading a filename out of post meta and substituting it into the attachment URL. It never touches the filesystem. The bug is a divergence between _wp_attachment_metadata in wp_postmeta and the files actually present under wp-content/uploads. The URL is the symptom; the metadata record is the fault.

Trace: the call chain from function to filename

The documented return value of wp_get_attachment_image_src() is an array whose element 0 is the image source URL, or false if no image is available. The function gets that array by calling image_downsize():

// wp-includes/media.php, wp_get_attachment_image_src()
$image = image_downsize( $attachment_id, $size );

image_downsize() is where the filename substitution happens. The function reference is explicit that it will not create a new resized copy: it returns an already resized one if it exists. The relevant lines:

// wp-includes/media.php, image_downsize()
$img_url = wp_get_attachment_url( $id );
$meta    = wp_get_attachment_metadata( $id );
$img_url_basename = wp_basename( $img_url );

$intermediate = image_get_intermediate_size( $id, $size );
if ( $intermediate ) {
    $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url );
    $width   = $intermediate['width'];
    $height  = $intermediate['height'];
    $is_intermediate = true;
}

Two things matter here. First, $meta comes from wp_get_attachment_metadata(), which reads the _wp_attachment_metadata post meta key. Second, the filename used in the URL comes from $intermediate['file'], which is a value inside that metadata array. There is no file_exists() call between the metadata read and the URL construction. The function trusts the record.

The metadata reference describes the shape: a sizes array keyed by size slug, each value containing file, width, height, and mime-type. The top-level file is a path relative to wp-content/uploads. The sizes entries are bare filenames that sit alongside that top-level file in the same year/month directory.

So the URL is assembled by string replacement: take the attachment URL (which ends in the top-level file basename), swap that basename for the size’s file value, and return the result. If the metadata says photo-1024x683.jpg exists, the function emits a URL ending in photo-1024x683.jpg. Whether that file is on disk is not part of the calculation.

Why migration breaks the pairing

The database and the uploads directory are two separate artifacts. A migration that copies one without the other, or copies both with different filters, leaves them out of sync. The metadata record survives because it lives in wp_postmeta; the file it names does not survive because it was excluded, renamed, or never copied.

The -scaled convention makes this worse. Since WordPress 5.3, an image whose width or height exceeds 2560 pixels is scaled down, the scaled copy is saved with -scaled appended to the filename, and that scaled copy becomes the file WordPress serves as the full-size image. The original is retained alongside it. According to SmartWP’s writeup of the feature, the original filename is stored in attachment metadata under original_image, and wp_get_original_image_path() / wp_get_original_image_url() return the unsuffixed file. ShortPixel’s explanation adds the detail that matters for this failure: once a scaled copy exists, WordPress rewrites _wp_attached_file to point at it, so get_attached_file() and wp_get_attachment_url() return the -scaled path, not the original.

That means a single large upload has at least two files on disk that the database references: the original (via original_image in metadata) and the scaled copy (via _wp_attached_file and the top-level file in metadata). A migration that copies only one of them produces exactly this class of mismatch. The metadata still names the scaled file; the scaled file is not there; wp_get_attachment_image_src($id, 'full') returns a URL to a missing file.

The same mechanism applies to sub-sizes. If the migration excluded intermediate sizes, or if a thumbnail regeneration ran against a different source than the one the metadata expects, the sizes array can name files that were never written. The function does not know the difference.

An illustrative scenario

This is a constructed example, not a case record. Suppose a migration script rsyncs uploads/ with --exclude='*-scaled.*'. Every large upload’s _wp_attached_file still points at a -scaled filename that no longer exists on the destination. wp_get_attachment_image_src($id, 'full') returns that URL. The browser 404s. The Media Library thumbnail still renders because a smaller sub-size did copy, which is why the attachment looks healthy in admin. The failure is invisible until a template requests the full size.

Diagnose the pair, not the URL

The correct diagnostic unit is the pair (attachment ID, size slug). For each pair, compare the filename in metadata against the actual directory listing. The URL is downstream of that comparison; fixing the URL without fixing the record leaves every other consumer broken.

Start with a single attachment. Dump the metadata and the resolved paths:

wp eval '
$id = 12345; // replace with the attachment ID
$meta = wp_get_attachment_metadata( $id );
$uploads = wp_upload_dir();
$dir = trailingslashit( $uploads["basedir"] ) . dirname( $meta["file"] );

printf( "top-level file: %s\n", $meta["file"] );
printf( "resolved dir:  %s\n", $dir );
printf( "top-level exists: %s\n", file_exists( $dir . "/" . basename( $meta["file"] ) ) ? "yes" : "NO" );

foreach ( $meta["sizes"] as $slug => $size ) {
    $path = $dir . "/" . $size["file"];
    printf( "%-20s %-40s %s\n", $slug, $size["file"], file_exists( $path ) ? "ok" : "MISSING" );
}
'

If the top-level file is missing, the attachment is broken at the root and no sub-size can be trusted. If only some sizes entries are missing, the metadata is stale for those slugs. Either way, the output tells you which files the database expects and which ones the filesystem has.

For a library-wide sweep, run the same comparison across all image attachments and write the mismatches to a log. This is a read-only pass; it does not modify anything:

wp eval '
$ids = get_posts( array(
    "post_type"      => "attachment",
    "post_mime_type" => "image",
    "post_status"    => "inherit",
    "posts_per_page" => -1,
    "fields"         => "ids",
) );
$uploads = wp_upload_dir();
$missing = 0;

foreach ( $ids as $id ) {
    $meta = wp_get_attachment_metadata( $id );
    if ( ! is_array( $meta ) || empty( $meta["file"] ) ) {
        continue;
    }
    $dir = trailingslashit( $uploads["basedir"] ) . dirname( $meta["file"] );
    $top = $dir . "/" . basename( $meta["file"] );
    if ( ! file_exists( $top ) ) {
        printf( "%d top-level missing: %s\n", $id, $meta["file"] );
        $missing++;
    }
    foreach ( (array) ( $meta["sizes"] ?? array() ) as $slug => $size ) {
        if ( ! file_exists( $dir . "/" . $size["file"] ) ) {
            printf( "%d %s missing: %s\n", $id, $slug, $size["file"] );
            $missing++;
        }
    }
}
printf( "total mismatches: %d\n", $missing );
'

Run this in a screen session on a large library. It reads every image attachment and stats every referenced file; on a site with tens of thousands of images it will take a while, and you do not want it dying halfway through because your SSH session dropped.

Fix the record, not the return value

The temptation is to add a filter on wp_get_attachment_image_src that returns false when the file is missing. Do not do this. The filter reference shows the hook receives the image array, the attachment ID, the size, and the icon flag. The same filter result is consumed by wp_get_attachment_image_srcset(), wp_get_attachment_image_sizes(), the REST attachments controller, and oEmbed. Returning false there degrades every consumer instead of repairing the record. It also hides the mismatch from the diagnostic pass, which means the next migration will reproduce it.

The repair is a metadata-vs-disk reconciliation. The shape of the fix depends on what is actually missing:

  • Top-level file missing, original present. The metadata points at a -scaled file that did not survive, but the original did. Point _wp_attached_file back at the original and rebuild metadata from it. This is the same operation the unscale.php snippet in the SmartWP writeup performs for scaled attachments: it calls update_attached_file() with the original path, then wp_update_attachment_metadata() with the result of wp_generate_attachment_metadata() against that original.
  • Top-level file missing, original also missing. The attachment is unrecoverable from disk. Either restore the file from backup or delete the attachment record. Regenerating against a missing source will fail per-attachment; it will not conjure the file.
  • Sub-sizes missing, top-level present. The metadata is stale for those slugs. Regeneration is the right tool, but only after confirming the source file exists at the path wp_get_original_image_path() returns. If the original is present, wp media regenerate --yes will rebuild the sub-sizes from it. If the original is missing and only the scaled copy survives, regeneration will rebuild from the scaled copy, which caps the maximum available resolution at the scaled dimensions and may permanently lose the reference to the true original.

Before running regeneration on any attachment, check the source path:

wp eval '
$id = 12345;
$original = wp_get_original_image_path( $id );
printf( "original path: %s\n", $original );
printf( "exists: %s\n", $original && file_exists( $original ) ? "yes" : "NO" );
'

If that prints NO, do not regenerate. Restore the file first. The metadata reference notes that wp_get_original_image_path() is among the functions that consume wp_get_attachment_metadata(), which is why the original_image key matters: it is the record of which file is the master copy. Lose it and regeneration has no correct source.

Verification

After reconciliation, re-run the diagnostic sweep. The mismatch count should be zero for the attachments you repaired. Then verify the specific URL that was failing:

wp eval '
$id = 12345;
$src = wp_get_attachment_image_src( $id, "full" );
if ( ! $src ) {
    echo "no image\n";
    exit;
}
printf( "url:    %s\n", $src[0] );
printf( "width:  %d\n", $src[1] );
printf( "height: %d\n", $src[2] );
printf( "resized: %s\n", $src[3] ? "yes" : "no" );

$path = wp_get_original_image_path( $id );
printf( "original exists: %s\n", $path && file_exists( $path ) ? "yes" : "NO" );
'

Fetch the URL with curl -I and confirm a 200. Then check the consumers that share the metadata: the REST attachment endpoint (/wp-json/wp/v2/media/12345) should return a source_url that resolves, and the block editor’s image block should render without a broken-image placeholder. If the REST response still points at a missing file, the reconciliation did not update the record that the REST controller reads; re-check that wp_update_attachment_metadata() actually wrote the new array.

Finally, fix the migration runbook. The database and the uploads tree are a single atomic unit. Copying wp_postmeta without the full uploads tree, including -scaled and -rotated variants, guarantees this failure class. Copying uploads without the database produces the inverse: orphan files with no metadata. Either way, the next migration will reproduce the problem unless the runbook treats them together.

FAQ

Does wp_get_attachment_image_src() check whether the file exists?

No. The function source delegates to image_downsize(), which reads the filename from _wp_attachment_metadata and substitutes it into the attachment URL. There is no filesystem check in that path. Adding one would stat a file on every image render, which is why core does not do it.

Why does the Media Library thumbnail still render when the full-size URL 404s?

The thumbnail is a separate sub-size with its own file entry in the sizes array. If that file copied during the migration and the full-size file did not, the thumbnail renders and the full-size request fails. The attachment looks healthy in admin because admin requests the thumbnail, not the full size.

Can I fix this by running wp media regenerate?

Only if the source file exists at the path wp_get_original_image_path() returns. Regeneration rebuilds sub-sizes from that source. If the source is missing and only the scaled copy survives, regeneration will rebuild from the scaled copy, which caps the maximum resolution and may lose the original_image reference. Check the source path first.

What is the -scaled suffix and why does it matter here?

Since WordPress 5.3, images over 2560 pixels on either edge are scaled down, the scaled copy gets -scaled appended to its filename, and that copy becomes the file WordPress serves as the full-size image. The original is retained alongside it and recorded in metadata under original_image. A migration that copies one but not the other produces exactly the metadata/filesystem divergence described here.

Should I add a filter to return false when the file is missing?

No. The wp_get_attachment_image_src filter result is consumed by srcset, sizes, the REST attachments controller, and oEmbed. Returning false degrades every consumer and hides the mismatch from the diagnostic pass. Repair the metadata record instead.