Basic WordPress Feed importer

Just a basic how-to here: making your own Feed importer tool to add some feeds in your WordPress themes. WordPress offers a bunch of useful tools to manage feeds, and among other cool stuffs, a method to import feeds : fetch_feed(). Given the feed’s url, you get back the feed content. Piece of cake 🙂 This goes in your theme’s functions.php:

function get_friends_feeds($friends = array(), $display = 2) {
    $feeds = array();
    if (count($friends) != 0) {
        foreach($friends as $friend) {
            $rss = fetch_feed($friend);
            if (!is_wp_error( $rss ) ) :
                $maxitems = $rss->get_item_quantity($display); 
                $rss_items = $rss->get_items(0, $maxitems); 
            if ($maxitems > 0) {
                foreach($rss_items as $item ) {
                    $feeds[$item->get_date('U')] = array('_href'  => esc_url($item->get_permalink()),
                                                         '_title' => esc_html($item->get_title()),
                                                         '_desc'  => strip_tags($item->get_description()),
                                                         '_date'  => $item->get_date('j<br />M'));
    return $feeds;

Note that this part only returns an array containing the feeds you want as objects; what to do with them depends on what you want. I use it to show me a simple bulleted list. This goes in my theme’s home.php, put it wherever you want:

<?php $feeds = get_friends_feeds(array('', '')); krsort($feeds); ?>
<?php if (count($feeds) != 0) : ?>
<?php foreach($feeds as $feed) : $feed = (object) $feed; ?>
              <?php echo $feed->_date; ?>: <a href="<?php echo $feed->_href; ?>" title="<?php echo $feed->_title; ?>"><?php echo $feed->_title; ?><br />
              <?php echo long_excerpt($feed->_desc, 25); ?></a>
<?php endforeach; ?>
<?php endif; ?>

get_friends_feeds() takes two parameters: first is an array containing all your feeds url, second is an integer to limit the returned items. Default is 2. Pretty simple way to get a few feeds to dynamize a bit your blog 🙂 I might make some Widget integration of that thing so that it can be include directly from Dashboard without editing the templates’ code. Feel free to use and abuse!

Note: the ksort() call on $feeds could be done directly into the get_friends_feeds() method; I chose not to do so to keep the liberty to order feeds’ items by date on a global scale or to let them ordered by feed if I want to. Anyway, you may modify this code however you want, so… Go ahead!

Publié par Charlie

Être humain depuis 1986, développeur web, designer et photographe, je code pour le Web depuis 2000 et pour WordPress depuis 2008. Aventure, Histoire, sciences, musique, café ou personnages forts en caractère et dotés d'un nez en tout point remarquable sont parmi mes passions les plus dévorantes. Indépendant depuis 2010 je travaille avec des gens formidables dans le monde entier, de la Californie à l'Europe en passant par l'Australie et l'Asie. D'autres détails croustillants ?

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.