Domain Substitution for WordPress
Substitutes the current domain with a custom domain for all attachment/image URLs; great for running a staging or local development site with images from your live/production site.
by Sully Syed · github.com/yllus/domain-substitution-for-wordpress · website
★ 0stars
0forks
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/yllus/domain-substitution-for-wordpress/archive/refs/heads/main.zipReadme
Domain Substitution for WordPress
A WordPress plugin that substitutes the current domain with a custom domain for all attachment/image URLs; great for running a staging or local development site with images from your live/production site.
Features
- Replaces site domain with custom domain for all attachment URLs
- Works with image srcset for responsive images
- Processes content to replace image URLs in posts and pages
- Perfect for staging/development environments using production images
- Simple settings page for easy configuration
- Lightweight with minimal performance impact
Installation
- Upload the
domain-substitution-for-wordpressfolder to/wp-content/plugins/ - Activate the plugin through the 'Plugins' menu in WordPress
- Go to Settings → WP Domain Substitution
- Enter your custom domain (e.g.,
cdn.example.comor your production site domain) - Save settings
Usage
For Site Administrators
- Navigate to Settings → WP Domain Substitution in your WordPress admin
- Enter the custom domain you want to use for images (without http:// or https://)
- Click "Save Changes"
- All attachment and image URLs will now use the custom domain
Common Use Cases
Staging Site with Production Images
- Problem: You're running a staging site but want to use images from your production site
- Solution: Set custom domain to your production domain (e.g.,
www.yoursite.com) - Result: Staging site loads images from production without duplicating files
Local Development
- Problem: Your local environment doesn't have all the media files
- Solution: Set custom domain to your production or staging domain
- Result: Local site loads images from remote server
CDN Configuration
- Problem: You want to serve images from a CDN
- Solution: Set custom domain to your CDN domain (e.g.,
cdn.example.com) - Result: All images load from CDN
For Developers
The plugin hooks into multiple WordPress filters to ensure comprehensive URL replacement:
Filters Used:
wp_get_attachment_url- Substitutes domain in attachment URLswp_get_attachment_image_src- Handles image source arrayswp_calculate_image_srcset- Processes responsive image srcsetsthe_content- Replaces domains in post/page content
How it works:
// The plugin replaces the site domain with custom domain
$site_url = parse_url(site_url(), PHP_URL_HOST);
$url = str_replace($site_url, $custom_domain, $url);
Domain Sanitization:
- Removes http:// or https:// protocol
- Trims whitespace
- Removes trailing slashes
- Sanitizes as text field
Requirements
- WordPress 5.0 or higher
- PHP 7.0 or higher
Changelog
1.0.0
- Initial release
- Domain substitution for attachment URLs
- Support for responsive images (srcset)
- Content filtering for embedded images
- Admin settings page
License
GPL v2 or later