Remote Image Caching
Here's how this works:
- Click the button below and we'll scan all of your posts for remote images
- Then you'll be presented with a checklist of domains, check the domains you want to grab cache from
- The images will be copied to your upload directory (this must be writable) and the links in your posts will be updated to the new location.
get_results("SELECT post_content FROM $wpdb->posts WHERE post_content LIKE ('%
|i', $post->post_content, $matches);
foreach ($matches[1] as $url) :
$url = parse_url($url);
$url['host'] = str_replace('www.', '', $url['host']);
$domains[$url['host']]++;
endforeach;
endforeach;
?>
We found some goodies. Check the domains that you want to grab images from:
get_results("SELECT post_content FROM $wpdb->posts WHERE post_content LIKE ('%
|i', $post->post_content, $matches);
foreach ( $matches[1] as $url ) :
if ( strstr( $url, get_option('siteurl') . '/' . get_option('upload_path') ) )
continue; // Already local
$filename = basename ( $url );
$b = parse_url( $url );
$dir = ABSPATH . get_option('upload_path') . '/' . $domain . dirname ( $b['path'] );
mkdirr( $dir );
$f = fopen( $dir . '/' . $filename , 'w' );
$img = file_get_contents( $b['scheme'] . '://' . $b['host'] . str_replace(' ', '%20', $b['path']) . $b['query'] );
if ( $img ) {
fwrite( $f, $img );
fclose( $f );
$local = get_option('siteurl') . '/' . get_option('upload_path') . '/' . $domain . dirname ( $b['path'] ) . "/$filename";
$wpdb->query("UPDATE $wpdb->posts SET post_content = REPLACE(post_content, '$url', '$local');");
echo "- Cached $url
";
flush();
}
endforeach;
endforeach;
?>
All done!