A simple WordPress plugin that redirects image URLs under wp-content/uploads/ on the current site to a production site URL. This is particularly useful for local or staging environments where you want to use production media without copying the entire uploads directory.
- Easy configuration: Admin screen for the production base URL and an on/off toggle
- HTTP Basic Authentication: Optional username and password when the production site is behind htpasswd-style protection
- Coverage:
- Attachment URLs,
wp_get_attachment_image_*, andwp_calculate_image_srcset - Post content (
the_content) - Classic and block widget content (
widget_text,widget_block_content) - Inline
srcsetandbackground-imageURLs in HTML (uploads paths only)
- Attachment URLs,
- Developer hooks (see below) to adjust behavior without forking
- Coding standards: WordPress Coding Standards via PHPCS (Composer)
- Upload the
production-image-redirectorfolder to/wp-content/plugins/ - Activate the plugin under Plugins
- Go to Settings → Image Redirector to configure
- Open Settings → Image Redirector
- Set Production Site URL (e.g.
https://yoursite.com) - Enable Enable Image Redirect
- If the production site uses HTTP Basic Authentication, set HTTP Authentication Username and HTTP Authentication Password (optional). Leaving the password field blank when saving keeps the stored password.
- Save changes
When both username and password are set, credentials are embedded in image URLs (https://user:pass@host/...) so the browser can load protected assets. Treat this as suitable for local/staging or low-risk setups. Credentials can be exposed via referrers, server logs, browser history, and anyone who can view page source or DevTools. Do not use high-value production secrets you would not put in a URL.
Only URLs that appear to point at the uploads directory (including a custom UPLOADS path) are rewritten. Other assets are left unchanged.
Example:
- Local:
http://localhost/wp-content/uploads/2024/01/image.jpg - Rewritten:
https://yoursite.com/wp-content/uploads/2024/01/image.jpg
All filters receive the current plugin settings array (except where noted).
- Type: filter
- Arguments:
(bool $allow, string $context, array $settings) - Context examples:
attachment_url,attachment_image_src,attachment_image_attributes,wp_calculate_image_srcset,the_content - Use: Disable redirection for specific requests (e.g. REST, cron) or contexts.
add_filter(
'production_image_redirector_should_redirect',
function ( $allow, $context, $settings ) {
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return false;
}
return $allow;
},
10,
3
);- Type: filter
- Arguments:
(string $new_url, string $original, array $settings) - Use: Adjust the final URL (signing, alternate host, stripping auth, etc.).
production-image-redirector/
├── production-image-redirector.php
├── uninstall.php
├── composer.json
├── phpcs.xml.dist
├── includes/
│ ├── constants.php
│ ├── class-admin.php
│ ├── class-url-redirector.php
│ ├── class-activator.php
│ └── index.php
├── languages/
│ └── index.php
└── README.md
- WordPress 5.8 or higher (block widget filter and general compatibility)
- PHP 7.4 or higher
composer install
composer lint # runs phpcs per composer.json "lint" scriptAuto-fix where possible:
composer lint:fix- Production URL is valid (typically
https://…) - Plugin is activated and Enable Image Redirect is checked
- The image path lives under uploads (see “How it works”)
- Production host is reachable (and Basic Auth credentials are correct if used)
GPL v2 or later — see License URI.
- HTTP Basic Authentication options for protected production sites
wp_calculate_image_srcsetsupport; fixsrcsetrewriting when the attribute appears aftersrc- Block widget support via
widget_block_content - Safer quoted
url("...")values for inline CSS background images - Filters:
production_image_redirector_should_redirect,production_image_redirector_redirect_url uninstall.phpwith multisite option cleanup viaget_sites()- Shared constants (
PRODUCTION_IMAGE_REDIRECTOR_*); plugin headers (Requires at least,Requires PHP,License URI) - WordPress Coding Standards tooling (Composer + PHPCS);
wp_parse_urlusage; request-level option cache in redirector - Removed unnecessary
flush_rewrite_ruleson activate/deactivate
- Initial release: admin settings, attachment and content redirects,
srcset/ background regex handling