A WordPress plugin that allows you to replace media files while maintaining their original URLs and metadata. This is particularly useful for updating files like PDFs, images, or other documents without breaking existing links.
- Replace media files while maintaining original URLs
- Preserves all existing links (both internal and external)
- Maintains file metadata and relationships
- Simple and intuitive interface in the WordPress Media Library
- Supports all file types supported by WordPress
- Validates file names to prevent accidental URL changes
- Enforces dimension matching for images to prevent layout issues
- Automatically handles WordPress scaled images
- Validates file type matching to ensure consistency
- AJAX-based replacement with error handling
- Download the plugin files
- Upload the
smart-media-replacementfolder to the/wp-content/plugins/directory - Activate the plugin through the 'Plugins' menu in WordPress
- Go to the WordPress Media Library
- Find the file you want to replace
- Click on the file to view its details
- Look for the "Replace File" button in the attachment details
- Click "Replace File" and select your new file
- The replacement will happen automatically
- The new file MUST have exactly the same filename as the original file
- Example: If your original file is
logo.png, your replacement must also be namedlogo.png - If filenames don't match, you'll receive an error with the required filename
- For images, the replacement MUST have identical dimensions (width × height)
- This prevents layout issues where images might break your design
- Example: If your original image is 1200×800px, your replacement must also be 1200×800px
- If WordPress automatically scaled your original image (large uploads), the plugin handles this automatically
- You should upload a file with the original filename (without
-scaled) - Example: If you see
photo-scaled.jpgin the media library, upload your replacement asphoto.jpg - The plugin will show you the correct filename if there's a mismatch
- The replacement file must be the same file type as the original
- Example: You cannot replace a
.jpgwith a.png - The plugin validates MIME types to ensure consistency
- Rename your replacement file to match the original filename exactly
- Check for the correct file extension
- Resize your replacement image to match the original dimensions
- Use image editing software to verify dimensions before uploading
- Your replacement file must be the same file type as the original
- Check that you're not trying to replace a JPEG with a PNG, or a PDF with a DOCX
- Your original image was larger than WordPress's threshold (typically 2560px)
- Upload your replacement with the original filename (shown in the error message)
- The plugin will handle the scaling automatically
- You must have permission to edit the specific media file
- Contact your site administrator if you believe you should have access
- Make sure you're viewing the attachment details (click on a media item)
- Clear your browser cache
- Check that you have permission to edit media files
smart_media_replacement_enforce_dimensions
Allows you to disable or customize dimension enforcement for specific attachments.
/**
* Disable dimension enforcement for specific attachments.
*
* @param bool $enforce Whether to enforce dimensions. Default true.
* @param int $attachment_id The attachment ID being replaced.
* @return bool
*/
add_filter( 'smart_media_replacement_enforce_dimensions', function( $enforce, $attachment_id ) {
// Allow flexible dimensions for attachment ID 123
if ( $attachment_id === 123 ) {
return false;
}
return $enforce;
}, 10, 2 );smart_media_replacement_file_replaced
Fires after a media file has been successfully replaced.
/**
* Perform custom actions after file replacement.
*
* @param int $attachment_id The ID of the attachment that was replaced.
* @param string $new_file_path The full path to the new file.
*/
add_action( 'smart_media_replacement_file_replaced', function( $attachment_id, $new_file_path ) {
// Clear custom caches
wp_cache_delete( 'my_custom_cache_' . $attachment_id );
// Notify external services
do_something_custom( $attachment_id );
// Log the replacement
error_log( "Media file replaced: {$attachment_id} -> {$new_file_path}" );
}, 10, 2 );- Clone the repository
- Install dependencies:
npm install composer install
- Build the JavaScript files:
npm run build
Functions/- PHP classes and WordPress hooksManageMedia.php- Core replacement functionality and AJAX handlers
src/- JavaScript source filesreplace-media.js- Frontend interaction and file upload handling
build/- Compiled JavaScript files (generated by webpack)vendor/- Composer dependencies
- The plugin uses WordPress's native media handling functions
- File validation happens server-side for security
- The JavaScript uses the WordPress i18n system for translations
- PHPCS is configured with WordPress coding standards
- WordPress 6.6 or higher
- PHP 7.0 or higher
- User must have
edit_postcapability for the specific attachment
This plugin:
- Does not collect or transmit any user data
- Does not use cookies
- Only processes files locally on your server
- Does not communicate with external services
For support, please open an issue on the GitHub repository.
See CHANGELOG.md for version history.
This plugin is licensed under the GPL v2 or later.
Developed by Troy Chaplin
