Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This might be really simple, but I'm pretty hopeless when it comes to rss and WP. Where should I put the if ( is_wp_error( $feed ) ) { in a code like this:

<?php 
                    $rss = fetch_feed('' . $instance["feed_address"] . '');
                    $maxitems = $rss->get_item_quantity($instance["feed_count"]); 
                    $rss_items = $rss->get_items(0, $maxitems); 
                    $title = substr($item['title'],0,55)." ... ";
                ?>

                <ul>
                    <?php if ($maxitems == 0) echo ''; else foreach ( $rss_items as $item ) : ?>
                    <li>
                        <h2><a class="description" href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo substr($item->get_title(), 0, 49) . ' ...'; ?> <span class="tip"><?php echo strip_tags(substr($item->get_description(), 0, 215)) . ' ...'; ?> <?php echo ''.$item->get_date('j F Y | g:i a'); ?></span></a></h2>
                    </li>
                    <?php endforeach; ?>
                </ul>

Thanks in advance.

EDIT:

I have the above code in a custom php page, not within core files. Does that make a difference? Anyhow, I managed to do this:

<?php 
                        $rss = fetch_feed('' . $instance["feed_address"] . '');
if ( !is_wp_error( $rss ) ) :
                        $maxitems = $rss->get_item_quantity($instance["feed_count"]); 
                        $rss_items = $rss->get_items(0, $maxitems); 
                        $title = substr($item['title'],0,55)." ... ";
endif;
                    ?>

                    <ul>
                        <?php if ($maxitems == 0) echo ''; else foreach ( $rss_items as $item ) : ?>
                        <li>
                            <h2><a class="description" href="<?php echo $item->get_permalink(); ?>" title="<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>" target="_blank"><?php echo substr($item->get_title(), 0, 49) . ' ...'; ?> <span class="tip"><?php echo strip_tags(substr($item->get_description(), 0, 215)) . ' ...'; ?></span></a></h2>
                        </li>
                        <?php endforeach; ?>
                    </ul>

The point being if ( !is_wp_error( $rss ) ) :, atleast it loads the feeds and everything seems to be working, but is this ok? The error may or may not appear from time to time, so testing is a bit hard. But before, when the error stroke, the feed stopped the loading that instant and did not load the other feed (I have two) after that either. With the code I posted above, will it skip the feed receiving the error and load the other one?

Thanks.

EDIT:

Thank you very much, without the code above, the error occurs as expected, but with it, the feed is just left empty and the next one is loaded like usual. You rock!

share|improve this question
    
Load an invalid URL to test it: fetch_feed( 'ftp://example.com'); –  toscho Mar 27 '11 at 0:36

1 Answer 1

Follow $rss = fetch_feed('' . $instance["feed_address"] . ''); and find the function in wp-includes/feed.php:

/**
 * Build SimplePie object based on RSS or Atom feed from URL.
 *
 * @since 2.8
 *
 * @param string $url URL to retrieve feed
 * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
 */
function fetch_feed($url) {
    require_once (ABSPATH . WPINC . '/class-feed.php');

    $feed = new SimplePie();
    $feed->set_feed_url($url);
    $feed->set_cache_class('WP_Feed_Cache');
    $feed->set_file_class('WP_SimplePie_File');
    $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 43200, $url));
    do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
    $feed->init();
    $feed->handle_content_type();

    if ( $feed->error() )
        return new WP_Error('simplepie-error', $feed->error());

    return $feed;
}

As you can see, fetch_feed() may return a WP_Error object. So check it just below the call to this function:

$rss = fetch_feed('' . $instance["feed_address"] . '');

if ( is_wp_error( $rss ) )
{
    // do something awesome
}
else
{
    // print your feed items
    // $maxitems = …
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.