Created
December 6, 2025 05:24
-
-
Save kupietools/c45a07aef7888e47a13097b6b8966ee5 to your computer and use it in GitHub Desktop.
Gather a flickr
This file contains hidden or 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
| /* This is a handler that overwrites the WordPress [gallery] shortcode handling, showing | |
| * the code that allows [gallery] to display images from Flickr galleries. | |
| * | |
| * In your WordPress content, you provide a shortcode parameter [gallery flickrid="12345"], | |
| * where 12345 is the ID of a Flickr gallery (from the gallery URL). | |
| * | |
| * You will need to fill in your Flickr API key where indicated near the beginning. | |
| * | |
| * This code creates its own $attachments array for later use in the gallery display HTML. | |
| * | |
| * Michael Kupietz | |
| * https://michaelkupietz.com | |
| * <[email protected]> | |
| */ | |
| add_filter("post_gallery", "my_post_gallery", 10, 2); //attach my_post_gallery function to filter post_gallery | |
| function my_post_gallery($output, $attr) | |
| { | |
| extract( | |
| shortcode_atts( | |
| [ | |
| "flickrid" => "", | |
| /* . | |
| . | |
| . | |
| rest of shortcode parameters go here | |
| . | |
| . | |
| . | |
| */ | |
| ], | |
| $attr | |
| ) | |
| ); | |
| //flickr code | |
| if ($flickrid !== "") { | |
| $flickr_url = "https://www.flickr.com/services/rest/?method=flickr.photosets.getPhotos" . | |
| "&api_key=[INSERT YOUR FLICKR API KEY HERE]" . | |
| "&photoset_id=" . urlencode($flickrid) . | |
| "&format=json&nojsoncallback=1"; | |
| $response = wp_remote_get($flickr_url); | |
| if (!is_wp_error($response)) { | |
| $photos = json_decode(wp_remote_retrieve_body($response), true); | |
| if (isset($photos['photoset']['photo'])) { | |
| $_attachments = []; | |
| foreach ($photos['photoset']['photo'] as $photo) { | |
| $attachment = new stdClass(); | |
| $attachment->ID = $photo['id']; | |
| // Create a fake wp_prepare_attachment_for_js response | |
| $img = [ | |
| 'id' => $photo['id'], | |
| 'title' => $photo['title'], | |
| 'filename' => $photo['title'], | |
| 'url' => "https://live.staticflickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_b.jpg", | |
| 'link' => "https://live.staticflickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_b.jpg", | |
| 'alt' => $photo['title'], | |
| 'sizes' => [ | |
| 'thumbnail' => [ | |
| 'url' => "https://live.staticflickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_q.jpg", | |
| 'width' => 150, | |
| 'height' => 150 | |
| ], | |
| 'medium' => [ | |
| 'url' => "https://live.staticflickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_z.jpg", | |
| 'width' => 640, | |
| 'height' => 640 | |
| ], | |
| 'full' => [ | |
| 'url' => "https://live.staticflickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_b.jpg", | |
| 'width' => 1024, | |
| 'height' => 1024 | |
| ] | |
| ] | |
| ]; | |
| // Add the wp_prepare_attachment_for_js data directly | |
| $attachment->prepared_js_data = $img; | |
| $_attachments[] = $attachment; | |
| } | |
| $attachments = []; | |
| foreach ($_attachments as $key => $val) { | |
| $attachments[$val->ID] = $_attachments[$key]; | |
| } | |
| // Override wp_prepare_attachment_for_js for these Flickr images | |
| add_filter('wp_prepare_attachment_for_js', function($response, $attachment) use ($attachments) { | |
| if (isset($attachments[$attachment->ID]) && isset($attachments[$attachment->ID]->prepared_js_data)) { | |
| return $attachments[$attachment->ID]->prepared_js_data; | |
| } | |
| return $response; | |
| }, 10, 2); | |
| } | |
| } | |
| } | |
| //end flickr code | |
| /* . | |
| . | |
| . | |
| rest of code to create HTML output of gallery goes here | |
| . | |
| . | |
| . | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment