Skip to content

Instantly share code, notes, and snippets.

@cfinke
Created October 30, 2014 14:23
Show Gist options
  • Save cfinke/a08a125bb789b251253d to your computer and use it in GitHub Desktop.
Save cfinke/a08a125bb789b251253d to your computer and use it in GitHub Desktop.
Retroactively sideload images into WordPress from Flickr. Used for a once-off. Not lots of error checking. Stick in mu-plugins and then load wp-admin/?do_flickr_sideload
<?php
/**
* Retroactively sideload images referenced from Flickr.
*/
$flickr_sideload_media_id = null;
function flickr_sideload_get_media_id( $media_id ) {
global $flickr_sideload_media_id;
$flickr_sideload_media_id = $media_id;
}
function flickr_sideload_init() {
global $flickr_sideload_media_id;
if ( isset( $_GET['do_flickr_sideload'] ) ) {
add_action( 'add_attachment', 'flickr_sideload_get_media_id' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$offset = 0;
$limit = 10;
while ( $posts = get_posts( 'posts_per_page=' . $limit . '&offset=' . $offset ) ) {
$offset += $limit;
foreach ( $posts as $post ) {
if ( preg_match_all( '/(?<atag><a[^>]+href="(?<href>[^"]+)"[^>]*>)\s*(?<imgtag><img[^>]+src="(?<src>[^"]+)"[^>]+>)\s*(<\/a>)?/i', $post->post_content, $images, PREG_SET_ORDER ) ) {
$post_content = $post->post_content;
foreach ( $images as $image ) {
if ( strpos( $image['src'], '.flickr.com/' ) !== false ) {
$description_matches = array();
$description = '';
if ( ! empty( $image['atag'] ) ) {
preg_match_all( '/title="(?<title>[^"]+)"/i', $image['atag'], $description_matches, PREG_SET_ORDER );
}
if ( ! empty( $description_matches ) ) {
$description = $description_matches[0]['title'];
}
else {
preg_match_all( '/alt="(?<alt>[^"]+)"/i', $image['imgtag'], $description_matches, PREG_SET_ORDER );
if ( ! empty( $description_matches ) ) {
$description = $description_matches[0]['alt'];
}
}
$image_html = media_sideload_image( $image['src'], $post->ID, $description );
preg_match_all( '/width="(?<width>[0-9]+)"/', $image['imgtag'], $width_matches, PREG_SET_ORDER );
preg_match_all( '/height="(?<height>[0-9]+)"/', $image['imgtag'], $height_matches, PREG_SET_ORDER );
if ( ! empty( $width_matches ) && ! empty( $height_matches ) ) {
$size = array( $width_matches[0]['width'], $height_matches[0]['height'] );
}
else {
$size = 'full';
}
if ( ! empty( $image['atag'] ) ) {
$image_html = wp_get_attachment_link( $flickr_sideload_media_id, $size );
}
else if ( $size != 'full' ) {
$image_html = str_replace( '<img ', '<img width="' . $size[0] . '" height="' . $size[1] . '" ', $image_html );
}
$post_content = str_replace( $image[0], $image_html, $post_content );
}
}
if ( $post_content != $post->post_content ) {
wp_update_post( array( 'ID' => $post->ID, 'post_content' => $post_content ) );
}
}
}
}
die;
}
}
add_action( 'init', 'flickr_sideload_init' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment