Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** Caching photos urls on save
- */
- add_action("save_post", function($post_id){
- if (get_current_screen()->post_type == "post"){
- // Reset
- if (!empty(get_post_meta($post_id, "cachedPhotos")))
- delete_post_meta($post_id, "cachedPhotos");
- $i = 0;
- $photosToChange = array();
- // Get photos
- $checkPhotos = get_post_meta($post_id, "cachedPhotos");
- // Process
- $content = get_post($post_id)->post_content;
- $pattern = get_shortcode_regex();
- preg_match_all("/".$pattern."/s", $content, $shortcodes);
- foreach ($shortcodes[0] as $photo){
- // ID
- preg_match_all('/id="([^"]+)"/', $photo, $id);
- if (isset($id[1][0])){
- $id = $id[1][0];
- $photosToChange[$i]["id"] = $id;
- }
- // Caption
- preg_match_all('/caption="([^"]+)"/', $photo, $caption);
- if (isset($caption[1][0])){
- $caption = $caption[1][0];
- $photosToChange[$i]["caption"] = $caption;
- }
- // If the ID is not empty
- if (isset($id[1][0])){
- // Flickr
- if (preg_match("/^\d{11}$/", $id)){
- $flickrJSON = file_get_contents("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=curiousyouare&photo_id={$id}&format=json&nojsoncallback=1");
- $photosToChange[$i]["link"] = "http://flickr.com/photos/fpeault/{$id}";
- $photosToChange[$i]["src"] = json_decode($flickrJSON)->sizes->size[7]->source;
- }
- // Instagram
- elseif (preg_match("/([a-zA-Z0-9_-]){10}/", $id)){
- $igJSON = file_get_contents("http://api.instagram.com/oembed?url=http://instagram.com/p/{$id}");
- $photosToChange[$i]["link"] = "http://instagram.com/p/{$id}";
- $photosToChange[$i]["src"] = json_decode($igJSON)->url;
- }
- }
- $i++;
- }
- add_post_meta($post_id, "cachedPhotos", $photosToChange);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement