Last active
November 14, 2015 08:40
-
-
Save sotospeak/41d27e67b299463b0eec to your computer and use it in GitHub Desktop.
Shortcode version of the wp core function, just a bit customized. Put this code anywhere in function.php
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 | |
function epy_widget_rss_output( $atts ) { | |
//where fetch_feed resides | |
include_once( ABSPATH . WPINC . '/feed.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>';} | |
} | |
//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