function lm_add_feature_image_to_rss($content) { global $post; $img_id = false; // From post meta, custom field $key = apply_filters("rss_image_meta_key", "blog-image"); if ($key && ($i = get_post_meta(get_the_ID(), $key, true))) { $img_id = image_get_attachment_id($i); } // Fall back to featured image if (!$img_id) { $img_id = get_post_thumbnail_id(); } // Fall back to first image from post content if (!$img_id && ($i = auto_archive_image())) { if ($i) { $img_id = image_get_attachment_id($i); } } // If we have an image URL, attach it if ($img_id) { $full_size = smart_media_size((int) $img_id, has_image_size('rssfeed-landscape') ? 'rssfeed-landscape' : 'medium'); $type = 'image/jpg'; if (substr($full_size, -3) == 'png') { $type = 'image/png'; } echo "\t"; echo sprintf('<media:content url="%s" medium="image" width="560" height="280" type="%s" />', esc_attr($full_size), esc_attr($type)); echo "\n"; } }
function auto_archive_image($post_id = false, $size = 'thumbnail') { if (!$post_id) { $post_id = get_the_ID(); } if (!$post_id) { return false; } // Use featured image if provided $featured = get_the_post_thumbnail($post_id, $size); if ($featured) { return $featured[0]; } // Look for an image in post meta. Meta key can be customized via filter or directly. $meta_key = apply_filters('ld-archive-thumbnail-meta-key', 'featured-image', $post_id, $size); if ($meta_key) { $meta_thumbnail = get_post_meta($post_id, $meta_key, true); if ($meta_thumbnail) { $img = false; if (is_numeric($meta_thumbnail)) { // Attachment ID $img = smart_media_size((int) $meta_thumbnail, $size); } else { if (is_string($meta_thumbnail)) { // Image URL $img = smart_media_size((string) $meta_thumbnail, $size); } } if ($img) { return $img; } } } // If we have already detected one through content before, use the cached version. $meta_thumbnail = get_post_meta($post_id, 'lm_thumbnail', true); if ($meta_thumbnail) { $img = smart_media_size((string) $meta_thumbnail, $size); if ($img) { return $img; } } // In case we have looked before but didn't find anything if ($meta_thumbnail === 0) { return false; } // We're going to scrape the post content and look for an image. We'll store it in lm_thumbnail if we find it so we don't have to scrape in the future. global $post; if ($post->ID == $post_id) { $content = $post->post_content; // Looks for an image, roughly checking if it is on this website // Regex looks like: <img*src=*(__frequentflyeracademy.com__)* if (!preg_match('/<img.*?src=[\'\\"](.*?' . $_SERVER['HTTP_HOST'] . '.*?)[\'\\"].*?>/i', $content, $matches)) { return false; } $attachment_id = image_get_attachment_id($matches[1]); if ($attachment_id) { // Attachment ID was found, return the thumbnail $attachment = wp_get_attachment_image_src($attachment_id, $size); if ($attachment) { $attachment_src = sprintf('<img src="%s" alt="%s - Featured Image" width="%s" height="%s" />', $attachment[0], esc_attr(get_the_title()), $attachment[1], $attachment[2]); update_post_meta($post_id, 'lm_thumbnail', $attachment_src); return $attachment_src; } } // No attachment found, cannot use a thumbnail. Return the full size image. $default_src = sprintf('<img src="%s" alt="%s - Featured Image" />', $matches[1], esc_attr(get_the_title())); update_post_meta($post_id, 'lm_thumbnail', $default_src); return $default_src; } update_post_meta($post_id, 'lm_thumbnail', 0); return false; }