fetch_feed

Build SimplePie object based on RSS or Atom feed from URL.

Description

(WP_Error|SimplePie) fetch_feed( (mixed) $url ); 

Returns (WP_Error|SimplePie)

WP_Error object on failure or SimplePie object on success

Parameters (1)

0. $url (mixed)
The url.

Options

    ;        


    Usage

    1. if ( !function_exists( 'fetch_feed' ) ) { 
    2. require_once ABSPATH . WPINC . '/feed.php'; 
    3.  
    4. // The url. 
    5. $url = null; 
    6.  
    7. // NOTICE! Understand what this does before running. 
    8. $result = fetch_feed($url); 
    9.  

    Defined (1)

    The function is defined in the following location(s).

    /wp-includes/feed.php  
    1. function fetch_feed( $url ) { 
    2. if ( ! class_exists( 'SimplePie', false ) ) { 
    3. require_once( ABSPATH . WPINC . '/class-simplepie.php' ); 
    4.  
    5. require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' ); 
    6. require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' ); 
    7. require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' ); 
    8. require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' ); 
    9.  
    10. $feed = new SimplePie(); 
    11.  
    12. $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' ); 
    13. // We must manually overwrite $feed->sanitize because SimplePie's 
    14. // constructor sets it before we have a chance to set the sanitization class 
    15. $feed->sanitize = new WP_SimplePie_Sanitize_KSES(); 
    16.  
    17. $feed->set_cache_class( 'WP_Feed_Cache' ); 
    18. $feed->set_file_class( 'WP_SimplePie_File' ); 
    19.  
    20. $feed->set_feed_url( $url ); 
    21. /** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */ 
    22. $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) ); 
    23. /** 
    24. * Fires just before processing the SimplePie feed object. 
    25. * @since 3.0.0 
    26. * @param object &$feed SimplePie feed object, passed by reference. 
    27. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged. 
    28. */ 
    29. do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) ); 
    30. $feed->init(); 
    31. $feed->set_output_encoding( get_option( 'blog_charset' ) ); 
    32.  
    33. if ( $feed->error() ) 
    34. return new WP_Error( 'simplepie-error', $feed->error() ); 
    35.  
    36. return $feed;