# Fast 404 for Missing Images, Scripts and Styles

> Answers requests for missing images, CSS, JavaScript and font files with a tiny 404 instead of loading a full theme page, saving server time.

- Type: PHP
- Runs: Run everywhere
- Category: [Performance](https://wpide.com/library/category/performance/)
- Tags: performance, 404, server
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/fast-404-static-files/

## Code

```php
// File extensions that should never be handled by WordPress pages.
$wpide_static_extensions = array( 'jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'svg', 'css', 'js', 'map', 'woff', 'woff2', 'ttf', 'eot', 'otf', 'mp4', 'webm', 'mp3' );

if ( ! is_admin() && ! ( defined( 'WP_CLI' ) && WP_CLI ) && isset( $_SERVER['REQUEST_URI'] ) ) {
    $wpide_path = (string) wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH );
    $wpide_ext  = strtolower( pathinfo( $wpide_path, PATHINFO_EXTENSION ) );

    if ( $wpide_ext && in_array( $wpide_ext, $wpide_static_extensions, true ) ) {
        status_header( 404 );
        nocache_headers();
        header( 'Content-Type: text/plain; charset=utf-8' );
        exit( 'Not found' );
    }
}
```

## Notes

Do not use it with plugins that create image sizes on the fly when a file is missing.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Fast 404 for Missing Images, Scripts and Styles" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
