Last active
June 15, 2016 19:13
-
-
Save jaygidwitz/089261b9787cc5ead75007076a278957 to your computer and use it in GitHub Desktop.
Add SI Feed To Site
This file contains hidden or 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
/** | |
* Register all dashboard metaboxes | |
* | |
* @access public | |
* @since 1.0 | |
* @return void | |
*/ | |
function rc_mdm_register_widgets() { | |
global $wp_meta_boxes; | |
wp_add_dashboard_widget('widget_studioissa', __('The Latest From Studio Issa', 'rc_mdm'), 'rc_mdm_create_my_rss_box'); | |
} | |
add_action('wp_dashboard_setup', 'rc_mdm_register_widgets'); | |
/** | |
* Creates the RSS metabox | |
* | |
* @access public | |
* @since 1.0 | |
* @return void | |
*/ | |
function rc_mdm_create_my_rss_box() { | |
// Get RSS Feed(s) | |
include_once(ABSPATH . WPINC . '/feed.php'); | |
// My feeds list (add your own RSS feeds urls) | |
$my_feeds = array( | |
'http://studioissa.com/feed/', | |
'http://designhelp.co/feed/', | |
'http://realestatemarketing.pro/blog/feed/' | |
); | |
// Loop through Feeds | |
foreach ( $my_feeds as $feed) : | |
// Get a SimplePie feed object from the specified feed source. | |
$rss = fetch_feed( $feed ); | |
if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly | |
// Figure out how many total items there are, and choose a limit | |
$maxitems = $rss->get_item_quantity( 3 ); | |
// Build an array of all the items, starting with element 0 (first element). | |
$rss_items = $rss->get_items( 0, $maxitems ); | |
// Get RSS title | |
$rss_title = '<a href="'.$rss->get_permalink().'" target="_blank">'.strtoupper( $rss->get_title() ).'</a>'; | |
endif; | |
// Display the container | |
echo '<div class="rss-widget">'; | |
echo '<strong>'.$rss_title.'</strong>'; | |
echo '<hr style="border: 0; background-color: #DFDFDF; height: 1px;">'; | |
// Starts items listing within <ul> tag | |
echo '<ul>'; | |
// Check items | |
if ( $maxitems == 0 ) { | |
echo '<li>'.__( 'No item', 'rc_mdm').'.</li>'; | |
} else { | |
// Loop through each feed item and display each item as a hyperlink. | |
foreach ( $rss_items as $item ) : | |
// Uncomment line below to display non human date | |
//$item_date = $item->get_date( get_option('date_format').' @ '.get_option('time_format') ); | |
// Get human date (comment if you want to use non human date) | |
$item_date = human_time_diff( $item->get_date('U'), current_time('timestamp')).' '.__( 'ago', 'rc_mdm' ); | |
// Start displaying item content within a <li> tag | |
echo '<li>'; | |
// create item link | |
echo '<a href="'.esc_url( $item->get_permalink() ).'" title="'.$item_date.'">'; | |
// Get item title | |
echo esc_html( $item->get_title() ); | |
echo '</a>'; | |
// Display date | |
echo ' <span class="rss-date">'.$item_date.'</span><br />'; | |
// Get item content | |
$content = $item->get_content(); | |
// Shorten content | |
$content = wp_html_excerpt($content, 120) . ' [...]'; | |
// Display content | |
echo $content; | |
// End <li> tag | |
echo '</li>'; | |
endforeach; | |
} | |
// End <ul> tag | |
echo '</ul></div>'; | |
endforeach; // End foreach feed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment