I’ve posted a code snippet I used to sideload images from Flickr into WordPress, specifically for http://foodirl.com/ I originally stored all of my images on Flickr because I started the blog on Tumblr, knowing that I would move it later (which I did, to Crowd Fusion and then to WordPress).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Stick it in mu-plugins
and then load wp-admin/?do_flickr_sideload
. It doesn’t have lots of error checking, but it worked flawlessly for FoodIRL’s 160+ posts.
A. I. Sajib 5:01 pm on November 4, 2014 Permalink |
Hi,
Thanks for your comment on my blog. I crawled back to your blog here and was curious if I could ask two questions regarding Flickr images on WordPress.
The first question is, is there any easier way that you know of that lets authors (editors and authors, to be precise) add single Flickr image from a preset account? I found WP Flickr Embed plugin to work exactly how I want it, but I’m hitting Ajax Error repeatedly. I searched their support forum but haven’t found a solution.
Secondly, do you know if there’s any easy way (a plugin would be handy) for us to replace all WordPress featured images (that are used as thumbnails on homepage and elsewhere on the site) by a source URL to a remote image? I store all my images to either Flickr or Google+ and link to them in order to reduce space and bandwidth on my hosting. It would make my life a lot easier if there was a way to not have to upload featured images in order for my posts to have a thumbnail image on the front page. I once worked for a publication that allowed something like this. They would add a custom field and a link to the remote image and that worked. I haven’t figured out how to do this easily.
I understand this can be difficult and may differentiate from theme to theme. But still I’m wondering if you know of any easier solution to either of the questions above.
Thanks for your time and answer.
Regards.
LikeLike
Christopher Finke 6:43 pm on November 4, 2014 Permalink |
I haven’t used anything that does this, but wp-flickr-press looks like it’s probably closest to what you want. Of you can just put the URL to the Flickr photo page (e.g., https://www.flickr.com/photos/cfinke/4291588352/) on its own line in your post, and WordPress will automatically embed it.
I’m not aware of one, but if you’re ok with hosting the images yourself, you can offload bandwidth to Automattic by using Jetpack’s Photon feature: http://jetpack.me/support/photon/
LikeLiked by 1 person
A. I. Sajib 2:15 am on November 5, 2014 Permalink
The embedding option is definitely amazing. The only problem is it doesn’t stretch to the full width of the content area. I wish there were ways to do this. I’m going to check out the plugin you linked to. Thanks for the recommendation.
I used Photon for a while but over time the more posts I publish the number of little image files increase on my server causing some sort of issues (I’m not very technically knowledgeable on server side issues). But thanks for your help. Perhaps I can convince someone to develop a plugin for me. ๐
Have a nice day.
LikeLike
A. I. Sajib 2:38 am on November 5, 2014 Permalink
Hi again,
I just wanted to say the plugin you recommended does exactly what I’m looking for. Though I needed to create an app on Flickr to get the API keys.
I can’t thank you enough for the link. ๐
LikeLike