Forked from sotospeak/wp_rss_feed_add_to_post_w_thumbs.php
Created
December 1, 2019 12:24
-
-
Save kengitahi/12e354edc47fe7d57828c6a3a46ed9ff to your computer and use it in GitHub Desktop.
Adding a feed to WP posts, scraping the featured images when thumbnails missing from feed
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
//put your feed in post/page with | |
//[add_rss url = "my feed uri" | |
//items = "number of items to retrieve" | |
//title = "title for" | |
//show_date = "show date or not" | |
//show_author "show author or not" | |
//show_summary = "whether to show a summary of each item"] | |
//declare the function | |
//from the original wp_widget_rss_output | |
//bit customized | |
//Some wp feeds miss thumbnails, so | |
//this version tries to scrape the featured image | |
//of feeed items, when missing and put them in html | |
function epy_widget_rss_output( $atts ) { | |
//where fetch_feed resides | |
include_once( ABSPATH . WPINC . '/feed.php' ); | |
//here using SImple HTML Dom Parser | |
//http://simplehtmldom.sourceforge.net/ | |
require_once(TEMPLATEPATH.'/lib/simple_html_dom.php'); | |
// My Attributes with defaults if missing | |
$atts=shortcode_atts( | |
array( | |
'url' => 'http://mysite/feed/rss2', | |
'items' => '4', | |
'title' => '', | |
'show_date' => '1', | |
'show_author' => '1', | |
'show_summary' => '1' | |
), $atts,'add_rss' ); | |
//[add_rss] will be my shortcode | |
//parse first attribute | |
$feedurl=esc_attr ($atts['url']); | |
// retrieve the feed with fetch_feed | |
$rss = fetch_feed($feedurl); | |
//if error | |
if ( is_wp_error($rss) ) { | |
if ( is_admin() || current_user_can('manage_options') ) | |
return '<p>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>'; | |
} | |
//parse items attribute | |
$items = (int) $atts[items]; | |
if ( $items < 1 || 20 < $items ) | |
$items = 10; | |
//parse other attributes, title is skipped as not useful in my case | |
$show_summary = (int) $atts[show_summary]; | |
$show_author = (int) $atts[show_author]; | |
$show_date = (int) $atts[show_date]; | |
//try if feed has some items if not end | |
if ( !$rss->get_item_quantity() ) { | |
return '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>'; | |
$rss->__destruct(); | |
unset($rss); | |
return; | |
} | |
//begin to build my output, remember you can not use echo in shortcode handlers | |
$myout="<ul class='scoop'>"; | |
//parse the items | |
foreach ( $rss->get_items(0, $items) as $item ) { | |
//parse the link | |
$link = $item->get_link(); | |
//sanitizing | |
while ( stristr($link, 'http') != $link ) | |
$link = substr($link, 1); | |
$link = esc_url(strip_tags($link)); | |
//parse the item title | |
$title = esc_attr(strip_tags($item->get_title())); | |
if ( empty($title) ) | |
$title = __('Untitled'); | |
//parse and sanitize the item summary | |
$desc = str_replace( array("\n", "\r"), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) ); | |
$excerpt = wp_html_excerpt( $desc, 170,' [...]' ); | |
// Append ellipsis. Change existing [...] to […]. | |
if ( '[...]' == substr( $excerpt, -5 ) ) | |
$excerpt = substr( $excerpt, 0, -5 ) . '[…]'; | |
elseif ( '[…]' != substr( $excerpt, -10 ) && $desc != $excerpt ) | |
$excerpt .= ' […]'; | |
$excerpt = esc_html( $excerpt ); | |
//prepare summary for output | |
if ( $show_summary ) { | |
$summary = "<div class='rssSummary2'><p>$excerpt</p></div>"; | |
} else { | |
$summary = ''; | |
} | |
//prepare date for output: you can customize html | |
$date = ''; | |
if ( $show_date ) { | |
$date = $item->get_date( 'U' ); | |
if ( $date ) { | |
$date = ' <span class="rss-date"><em>' . date_i18n( get_option( 'date_format' ), $date ) . '</em></span>'; | |
} | |
} | |
//prepare author for output: you can customize html | |
$author = ''; | |
if ( $show_author ) { | |
$author = $item->get_author(); | |
if ( is_object($author) ) { | |
$author = $author->get_name(); | |
$author = ' <span class="rss-author"><em>by ' . esc_html( strip_tags( $author ) ) . '</em></span>'; | |
} | |
} | |
//check if an item thumbnail is present | |
if ($enclosure = $item->get_enclosure()) | |
{ | |
$link_img=$enclosure->get_link(); | |
if ($link_img != '') { | |
$mythumb= '<div><img src="' . $link_img . '" class="feed-thumb" /></div>'; | |
} | |
//if thumb missing in feed | |
else { | |
$myhtml = file_get_html($link); | |
//capture on item's page url | |
//image with class=attachment-post-thumbnail | |
//usually this is the featured image | |
$myelement = $myhtml->find('img[class=attachment-post-thumbnail]'); | |
$link_img_parsed=$myelement[0]->src; | |
$mythumb= '<div><img src="' . $link_img_parsed. '" class="feed-thumb" /></div>'; | |
} | |
} | |
//add to output this complete item | |
$myout=$myout."<li><h2 class='rss-title'><a class='rsswidget' href='$link' title='$desc' target='_blank'>$title</a></h2><div>{$mythumb}</div><div class='rss-meta'>{$date}{$author}</div><div>{$summary}</div></li>"; | |
} | |
//close list as items finish | |
$myout=$myout."</ul>"; | |
$rss->__destruct(); | |
unset($rss); | |
return $myout; | |
} | |
// Add Shortcode | |
add_shortcode( 'add_rss', 'epy_widget_rss_output' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment