Created
June 8, 2018 02:48
-
-
Save ngearing/171dc8f29c2bafd195e69acb0b524852 to your computer and use it in GitHub Desktop.
Add media and images to rss feed in 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 | |
/** | |
* Add 'Featured Image' to a rss feed. | |
* | |
* @return void | |
*/ | |
function add_feed_item() { | |
$query = get_queried_object(); | |
// Add featured image to 'Featured' events. | |
if ( | |
'tribe_events' === get_post_type() && | |
is_a( $query, 'WP_Term' ) && | |
'featured' === $query->slug | |
) { | |
$featured_image = get_post_thumbnail_id( get_the_id() ); | |
if ( $featured_image ) { | |
$image = wp_get_attachment_image_src( $featured_image, 'thumbnail' ); | |
list($url, $width, $height) = $image; | |
$image_path = str_replace( home_url(), ABSPATH, $url ); | |
$mime = getimagesize( $image_path )['mime']; | |
printf( | |
'<media:content url="%s" type="%s" height="%s" width="%s" />', | |
$url, | |
$mime, | |
$height, | |
$width | |
); | |
} | |
} | |
} | |
add_action( 'rss2_item', 'add_feed_item' ); | |
/** | |
* Add Media namespace to feed. | |
* | |
* The media namespace is required to make the <media> tag valid for readers. | |
* | |
* @return void | |
*/ | |
function feed_namespace() { | |
echo 'xmlns:media="http://search.yahoo.com/mrss/"'; | |
} | |
add_action( 'rss2_ns', 'feed_namespace' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment