belloli

WordPress: Shortcode for display Feed RSS

Mar 3rd, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. <?php
  2. /*
  3.  ********************************************************************************
  4.  *                                                                              *
  5.  * WordPress: Shortcode for display Feed RSS                    *
  6.  * Author: Lucas S. B. Mastella <lucas[at]bello[dot]li>                         *
  7.  * Site: http://bello.li/                           *
  8.  * Date: 03/03/2012                                                             *
  9.  *                                                                              *
  10.  ********************************************************************************
  11.  *                                                                              *
  12.  *  Attribute:  URL, NUM, TARGET                        *
  13.  *  Default: NUM=3, TARGET="blank"                      *
  14.  *      num     = [0-9]^                        *
  15.  *      target  = [blank, self, parent, top]                *
  16.  *                                      *
  17.  *  Ex.: [feed url="http://bello.li/feed/" num=5 target="self"]     *
  18.  *                                                                              *
  19.  ********************************************************************************
  20.  *                                                                              *
  21.  * This program is free software: you can redistribute it and/or modify         *
  22.  * it under the terms of the GNU General Public License as published by         *
  23.  * the Free Software Foundation, either version 3 of the License, or            *
  24.  * (at your option) any later version.                                          *
  25.  *                                                                              *
  26.  * This program is distributed in the hope that it will be useful.              *
  27.  * but WITHOUT ANY WARRANTY; without even the implied warranty of               *
  28.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                *
  29.  * GNU General Public License for more details.                                 *
  30.  *                                                                              *
  31.  * You should have received a copy of the GNU General Public License            *
  32.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.        *
  33.  *                                                                              *
  34.  ********************************************************************************
  35.  *
  36.  * Thank's to:
  37.  * RSS Widget doesn’t update: http://newtotheweb.net/2009/10/25/rss-widget-doesnt-update/
  38.  *
  39.  */
  40.  
  41. function feed_fetcher( $atts ) {
  42.     // Update Cache
  43.     add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', 'return 0;' ) );
  44.  
  45.     // Shortcode Attribute
  46.     extract( shortcode_atts( array(
  47.         'url' => '',
  48.         'num' => '3',
  49.         'target' => 'blank'
  50.     ), $atts ) );
  51.  
  52.     // Fetch Feed
  53.     include_once( ABSPATH . WPINC . '/feed.php' );
  54.         $rss = fetch_feed( $url );
  55.         $max = $rss->get_item_quantity( $num );
  56.         $rss_itens = $rss->get_items( 0, $max );
  57.  
  58.         if ( $max == 0 ) echo 'No URL has been added!';
  59.         else
  60.             foreach ( $rss_itens as $item ) :
  61.                 echo '<a href="' . $item->get_permalink() . '" target="_' . $target . '">' . $item->get_title() . '</a><br />';
  62.             endforeach;
  63. }
  64. add_shortcode( 'feed', 'feed_fetcher' );
  65. ?>
Add Comment
Please, Sign In to add comment