5 * Many of the functions used in here belong in The Loop, or The Loop for the
14 * RSS container for the bloginfo function.
16 * You can retrieve anything that you can using the get_bloginfo() function.
17 * Everything will be stripped of tags and characters converted, when the values
18 * are retrieved for use in the feeds.
21 * @see get_bloginfo() For the list of possible values to display.
23 * @param string $show See get_bloginfo() for possible values.
26 function get_bloginfo_rss($show = '') {
27 $info = strip_tags(get_bloginfo($show));
29 * Filters the bloginfo for use in RSS feeds.
33 * @see convert_chars()
36 * @param string $info Converted string value of the blog information.
37 * @param string $show The type of blog information to retrieve.
39 return apply_filters( 'get_bloginfo_rss', convert_chars( $info ), $show );
43 * Display RSS container for the bloginfo function.
45 * You can retrieve anything that you can using the get_bloginfo() function.
46 * Everything will be stripped of tags and characters converted, when the values
47 * are retrieved for use in the feeds.
50 * @see get_bloginfo() For the list of possible values to display.
52 * @param string $show See get_bloginfo() for possible values.
54 function bloginfo_rss($show = '') {
56 * Filters the bloginfo for display in RSS feeds.
62 * @param string $rss_container RSS container for the blog information.
63 * @param string $show The type of blog information to retrieve.
65 echo apply_filters( 'bloginfo_rss', get_bloginfo_rss( $show ), $show );
69 * Retrieve the default feed.
71 * The default feed is 'rss2', unless a plugin changes it through the
72 * {@see 'default_feed'} filter.
76 * @return string Default feed, or for example 'rss2', 'atom', etc.
78 function get_default_feed() {
80 * Filters the default feed type.
84 * @param string $feed_type Type of default feed. Possible values include 'rss2', 'atom'.
87 $default_feed = apply_filters( 'default_feed', 'rss2' );
88 return 'rss' == $default_feed ? 'rss2' : $default_feed;
92 * Retrieve the blog title for the feed title.
95 * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`.
97 * @param string $deprecated Unused..
98 * @return string The document title.
100 function get_wp_title_rss( $deprecated = '–' ) {
101 if ( '–' !== $deprecated ) {
102 /* translators: %s: 'document_title_separator' filter name */
103 _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) );
107 * Filters the blog title for use as the feed title.
110 * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`.
112 * @param string $title The current blog title.
113 * @param string $deprecated Unused.
115 return apply_filters( 'get_wp_title_rss', wp_get_document_title(), $deprecated );
119 * Display the blog title for display of the feed title.
122 * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`.
124 * @param string $deprecated Unused.
126 function wp_title_rss( $deprecated = '–' ) {
127 if ( '–' !== $deprecated ) {
128 /* translators: %s: 'document_title_separator' filter name */
129 _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) );
133 * Filters the blog title for display of the feed title.
136 * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`.
138 * @see get_wp_title_rss()
140 * @param string $wp_title_rss The current blog title.
141 * @param string $deprecated Unused.
143 echo apply_filters( 'wp_title_rss', get_wp_title_rss(), $deprecated );
147 * Retrieve the current post title for the feed.
151 * @return string Current post title.
153 function get_the_title_rss() {
154 $title = get_the_title();
157 * Filters the post title for use in a feed.
161 * @param string $title The current post title.
163 $title = apply_filters( 'the_title_rss', $title );
168 * Display the post title in the feed.
172 function the_title_rss() {
173 echo get_the_title_rss();
177 * Retrieve the post content for feeds.
180 * @see get_the_content()
182 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
183 * @return string The filtered content.
185 function get_the_content_feed($feed_type = null) {
187 $feed_type = get_default_feed();
189 /** This filter is documented in wp-includes/post-template.php */
190 $content = apply_filters( 'the_content', get_the_content() );
191 $content = str_replace(']]>', ']]>', $content);
193 * Filters the post content for use in feeds.
197 * @param string $content The current post content.
198 * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
201 return apply_filters( 'the_content_feed', $content, $feed_type );
205 * Display the post content for feeds.
209 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
211 function the_content_feed($feed_type = null) {
212 echo get_the_content_feed($feed_type);
216 * Display the post excerpt for the feed.
220 function the_excerpt_rss() {
221 $output = get_the_excerpt();
223 * Filters the post excerpt for a feed.
227 * @param string $output The current post excerpt.
229 echo apply_filters( 'the_excerpt_rss', $output );
233 * Display the permalink to the post for use in feeds.
237 function the_permalink_rss() {
239 * Filters the permalink to the post for use in feeds.
243 * @param string $post_permalink The current post permalink.
245 echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
249 * Outputs the link to the comments for the current post in an xml safe way
254 function comments_link_feed() {
256 * Filters the comments permalink for the current post.
260 * @param string $comment_permalink The current comment permalink with
261 * '#comments' appended.
263 echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) );
267 * Display the feed GUID for the current comment.
271 * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
273 function comment_guid($comment_id = null) {
274 echo esc_url( get_comment_guid($comment_id) );
278 * Retrieve the feed GUID for the current comment.
282 * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
283 * @return false|string false on failure or guid for comment on success.
285 function get_comment_guid($comment_id = null) {
286 $comment = get_comment($comment_id);
288 if ( !is_object($comment) )
291 return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
295 * Display the link to the comments.
298 * @since 4.4.0 Introduced the `$comment` argument.
300 * @param int|WP_Comment $comment Optional. Comment object or id. Defaults to global comment object.
302 function comment_link( $comment = null ) {
304 * Filters the current comment's permalink.
308 * @see get_comment_link()
310 * @param string $comment_permalink The current comment permalink.
312 echo esc_url( apply_filters( 'comment_link', get_comment_link( $comment ) ) );
316 * Retrieve the current comment author for use in the feeds.
320 * @return string Comment Author
322 function get_comment_author_rss() {
324 * Filters the current comment author for use in a feed.
328 * @see get_comment_author()
330 * @param string $comment_author The current comment author.
332 return apply_filters( 'comment_author_rss', get_comment_author() );
336 * Display the current comment author in the feed.
340 function comment_author_rss() {
341 echo get_comment_author_rss();
345 * Display the current comment content for use in the feeds.
349 function comment_text_rss() {
350 $comment_text = get_comment_text();
352 * Filters the current comment content for use in a feed.
356 * @param string $comment_text The content of the current comment.
358 $comment_text = apply_filters( 'comment_text_rss', $comment_text );
363 * Retrieve all of the post categories, formatted for use in feeds.
365 * All of the categories for the current post in the feed loop, will be
366 * retrieved and have feed markup added, so that they can easily be added to the
367 * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
371 * @param string $type Optional, default is the type returned by get_default_feed().
372 * @return string All of the post categories for displaying in the feed.
374 function get_the_category_rss($type = null) {
376 $type = get_default_feed();
377 $categories = get_the_category();
378 $tags = get_the_tags();
380 $cat_names = array();
383 if ( 'atom' == $type )
386 if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
387 $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
390 if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
391 $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
394 $cat_names = array_unique($cat_names);
396 foreach ( $cat_names as $cat_name ) {
397 if ( 'rdf' == $type )
398 $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
399 elseif ( 'atom' == $type )
400 $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
402 $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
406 * Filters all of the post categories for display in a feed.
410 * @param string $the_list All of the RSS post categories.
411 * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
414 return apply_filters( 'the_category_rss', $the_list, $type );
418 * Display the post categories in the feed.
421 * @see get_the_category_rss() For better explanation.
423 * @param string $type Optional, default is the type returned by get_default_feed().
425 function the_category_rss($type = null) {
426 echo get_the_category_rss($type);
430 * Display the HTML type based on the blog setting.
432 * The two possible values are either 'xhtml' or 'html'.
436 function html_type_rss() {
437 $type = get_bloginfo('html_type');
438 if (strpos($type, 'xhtml') !== false)
446 * Display the rss enclosure for the current post.
448 * Uses the global $post to check whether the post requires a password and if
449 * the user has the password for the post. If not then it will return before
452 * Also uses the function get_post_custom() to get the post's 'enclosure'
453 * metadata field and parses the value to display the enclosure(s). The
454 * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
459 function rss_enclosure() {
460 if ( post_password_required() )
463 foreach ( (array) get_post_custom() as $key => $val) {
464 if ($key == 'enclosure') {
465 foreach ( (array) $val as $enc ) {
466 $enclosure = explode("\n", $enc);
468 // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
469 $t = preg_split('/[ \t]/', trim($enclosure[2]) );
473 * Filters the RSS enclosure HTML link tag for the current post.
477 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
479 echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
486 * Display the atom enclosure for the current post.
488 * Uses the global $post to check whether the post requires a password and if
489 * the user has the password for the post. If not then it will return before
492 * Also uses the function get_post_custom() to get the post's 'enclosure'
493 * metadata field and parses the value to display the enclosure(s). The
494 * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
498 function atom_enclosure() {
499 if ( post_password_required() )
502 foreach ( (array) get_post_custom() as $key => $val ) {
503 if ($key == 'enclosure') {
504 foreach ( (array) $val as $enc ) {
505 $enclosure = explode("\n", $enc);
507 * Filters the atom enclosure HTML link tag for the current post.
511 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
513 echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
520 * Determine the type of a string of data with the data formatted.
522 * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
524 * In the case of WordPress, text is defined as containing no markup,
525 * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
527 * Container div tags are added to xhtml values, per section 3.1.1.3.
529 * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
533 * @param string $data Input string
534 * @return array array(type, value)
536 function prep_atom_text_construct($data) {
537 if (strpos($data, '<') === false && strpos($data, '&') === false) {
538 return array('text', $data);
541 if ( ! function_exists( 'xml_parser_create' ) ) {
542 trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
544 return array( 'html', "<![CDATA[$data]]>" );
547 $parser = xml_parser_create();
548 xml_parse($parser, '<div>' . $data . '</div>', true);
549 $code = xml_get_error_code($parser);
550 xml_parser_free($parser);
553 if (strpos($data, '<') === false) {
554 return array('text', $data);
556 $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
557 return array('xhtml', $data);
561 if (strpos($data, ']]>') === false) {
562 return array('html', "<![CDATA[$data]]>");
564 return array('html', htmlspecialchars($data));
569 * Displays Site Icon in atom feeds.
573 * @see get_site_icon_url()
575 function atom_site_icon() {
576 $url = get_site_icon_url( 32 );
578 echo "<icon>$url</icon>\n";
583 * Displays Site Icon in RSS2.
587 function rss2_site_icon() {
588 $rss_title = get_wp_title_rss();
589 if ( empty( $rss_title ) ) {
590 $rss_title = get_bloginfo_rss( 'name' );
593 $url = get_site_icon_url( 32 );
597 <url>' . convert_chars( $url ) . '</url>
598 <title>' . $rss_title . '</title>
599 <link>' . get_bloginfo_rss( 'url' ) . '</link>
607 * Display the link for the currently displayed feed in a XSS safe way.
609 * Generate a correct link for the atom:self element.
613 function self_link() {
614 $host = @parse_url(home_url());
616 * Filters the current feed URL.
620 * @see set_url_scheme()
623 * @param string $feed_link The link for the feed with set URL scheme.
625 echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
629 * Return the content type for specified feed type.
633 * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
635 function feed_content_type( $type = '' ) {
637 $type = get_default_feed();
640 'rss' => 'application/rss+xml',
641 'rss2' => 'application/rss+xml',
642 'rss-http' => 'text/xml',
643 'atom' => 'application/atom+xml',
644 'rdf' => 'application/rdf+xml'
647 $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
650 * Filters the content type for a specific feed type.
654 * @param string $content_type Content type indicating the type of data that a feed contains.
655 * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
657 return apply_filters( 'feed_content_type', $content_type, $type );
661 * Build SimplePie object based on RSS or Atom feed from URL.
665 * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
666 * using SimplePie's multifeed feature.
667 * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
669 * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
671 function fetch_feed( $url ) {
672 if ( ! class_exists( 'SimplePie', false ) ) {
673 require_once( ABSPATH . WPINC . '/class-simplepie.php' );
676 require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
677 require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
678 require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
679 require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );
681 $feed = new SimplePie();
683 $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
684 // We must manually overwrite $feed->sanitize because SimplePie's
685 // constructor sets it before we have a chance to set the sanitization class
686 $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
688 $feed->set_cache_class( 'WP_Feed_Cache' );
689 $feed->set_file_class( 'WP_SimplePie_File' );
691 $feed->set_feed_url( $url );
692 /** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
693 $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
695 * Fires just before processing the SimplePie feed object.
699 * @param object &$feed SimplePie feed object, passed by reference.
700 * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
702 do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
704 $feed->set_output_encoding( get_option( 'blog_charset' ) );
706 if ( $feed->error() )
707 return new WP_Error( 'simplepie-error', $feed->error() );