At first, this may not seem to have any real-world use, but consider this: You need to set up an independent development environment for your site, and you’d rather not copy gigabytes of files from your production server’s wp-content directory. Without the following function, keeping everything synced could turn into another full-time job, especially if your editorial staff is posting hundreds upon hundreds of articles each day.
Here’s how it works: This filter interrupts the output of wp_get_attachment_image and replaces the returned image tag’s src attribute with your production server’s url, therefore effectively telling the page to read images from a different server than the page is served from.
So here’s the WordPress code that makes it all happen. Put it in a file in your development server’s mu-plugins directory so it doesn’t get overwritten with theme synchronizations. Remember to swap out “yourserver.com” and “dev.yourserver.com” with appropriate values.
[code language=”php”]
<?php
function rewrite_images( $atts ) {
$atts[‘src’] = str_replace( ‘dev.yourserver.com’, ‘yourserver.com’, $atts[‘src’] );
return $atts;
}
add_filter( ‘wp_get_attachment_image_attributes’, ‘rewrite_images’ );
?>[/code]
What other creative uses can you think of for this snippet? Leave me a comment below!
Leave a Reply to RichCancel reply