5 * Many of the functions used in here belong in The Loop, or The Loop for the
13 * RSS container for the bloginfo function.
15 * You can retrieve anything that you can using the get_bloginfo() function.
16 * Everything will be stripped of tags and characters converted, when the values
17 * are retrieved for use in the feeds.
20 * @see get_bloginfo() For the list of possible values to display.
22 * @param string $show See get_bloginfo() for possible values.
25 function get_bloginfo_rss($show = '') {
26 $info = strip_tags(get_bloginfo($show));
28 * Filter the bloginfo for use in RSS feeds.
32 * @see convert_chars()
35 * @param string $info Converted string value of the blog information.
36 * @param string $show The type of blog information to retrieve.
38 return apply_filters( 'get_bloginfo_rss', convert_chars( $info ), $show );
42 * Display RSS container for the bloginfo function.
44 * You can retrieve anything that you can using the get_bloginfo() function.
45 * Everything will be stripped of tags and characters converted, when the values
46 * are retrieved for use in the feeds.
49 * @see get_bloginfo() For the list of possible values to display.
51 * @param string $show See get_bloginfo() for possible values.
53 function bloginfo_rss($show = '') {
55 * Filter the bloginfo for display in RSS feeds.
61 * @param string $rss_container RSS container for the blog information.
62 * @param string $show The type of blog information to retrieve.
64 echo apply_filters( 'bloginfo_rss', get_bloginfo_rss( $show ), $show );
68 * Retrieve the default feed.
70 * The default feed is 'rss2', unless a plugin changes it through the
71 * 'default_feed' filter.
75 * @return string Default feed, or for example 'rss2', 'atom', etc.
77 function get_default_feed() {
79 * Filter the default feed type.
83 * @param string $feed_type Type of default feed. Possible values include 'rss2', 'atom'.
86 $default_feed = apply_filters( 'default_feed', 'rss2' );
87 return 'rss' == $default_feed ? 'rss2' : $default_feed;
91 * Retrieve the blog title for the feed title.
94 * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`.
96 * @param string $deprecated Unused..
97 * @return string The document title.
99 function get_wp_title_rss( $deprecated = '–' ) {
100 if ( '–' !== $deprecated ) {
101 /* translators: %s: 'document_title_separator' filter name */
102 _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) );
106 * Filter the blog title for use as the feed title.
109 * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`.
111 * @param string $title The current blog title.
112 * @param string $deprecated Unused.
114 return apply_filters( 'get_wp_title_rss', wp_get_document_title(), $deprecated );
118 * Display the blog title for display of the feed title.
121 * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`.
123 * @param string $deprecated Unused.
125 function wp_title_rss( $deprecated = '–' ) {
126 if ( '–' !== $deprecated ) {
127 /* translators: %s: 'document_title_separator' filter name */
128 _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) );
132 * Filter the blog title for display of the feed title.
135 * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`.
137 * @see get_wp_title_rss()
139 * @param string $wp_title_rss The current blog title.
140 * @param string $deprecated Unused.
142 echo apply_filters( 'wp_title_rss', get_wp_title_rss(), $deprecated );
146 * Retrieve the current post title for the feed.
150 * @return string Current post title.
152 function get_the_title_rss() {
153 $title = get_the_title();
156 * Filter the post title for use in a feed.
160 * @param string $title The current post title.
162 $title = apply_filters( 'the_title_rss', $title );
167 * Display the post title in the feed.
171 function the_title_rss() {
172 echo get_the_title_rss();
176 * Retrieve the post content for feeds.
179 * @see get_the_content()
181 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
182 * @return string The filtered content.
184 function get_the_content_feed($feed_type = null) {
186 $feed_type = get_default_feed();
188 /** This filter is documented in wp-includes/post-template.php */
189 $content = apply_filters( 'the_content', get_the_content() );
190 $content = str_replace(']]>', ']]>', $content);
192 * Filter the post content for use in feeds.
196 * @param string $content The current post content.
197 * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
200 return apply_filters( 'the_content_feed', $content, $feed_type );
204 * Display the post content for feeds.
208 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
210 function the_content_feed($feed_type = null) {
211 echo get_the_content_feed($feed_type);
215 * Display the post excerpt for the feed.
219 function the_excerpt_rss() {
220 $output = get_the_excerpt();
222 * Filter the post excerpt for a feed.
226 * @param string $output The current post excerpt.
228 echo apply_filters( 'the_excerpt_rss', $output );
232 * Display the permalink to the post for use in feeds.
236 function the_permalink_rss() {
238 * Filter the permalink to the post for use in feeds.
242 * @param string $post_permalink The current post permalink.
244 echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
248 * Outputs the link to the comments for the current post in an xml safe way
253 function comments_link_feed() {
255 * Filter the comments permalink for the current post.
259 * @param string $comment_permalink The current comment permalink with
260 * '#comments' appended.
262 echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) );
266 * Display the feed GUID for the current comment.
270 * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
272 function comment_guid($comment_id = null) {
273 echo esc_url( get_comment_guid($comment_id) );
277 * Retrieve the feed GUID for the current comment.
281 * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
282 * @return false|string false on failure or guid for comment on success.
284 function get_comment_guid($comment_id = null) {
285 $comment = get_comment($comment_id);
287 if ( !is_object($comment) )
290 return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
294 * Display the link to the comments.
297 * @since 4.4.0 Introduced the `$comment` argument.
299 * @param int|WP_Comment $comment Optional. Comment object or id. Defaults to global comment object.
301 function comment_link( $comment = null ) {
303 * Filter the current comment's permalink.
307 * @see get_comment_link()
309 * @param string $comment_permalink The current comment permalink.
311 echo esc_url( apply_filters( 'comment_link', get_comment_link( $comment ) ) );
315 * Retrieve the current comment author for use in the feeds.
319 * @return string Comment Author
321 function get_comment_author_rss() {
323 * Filter the current comment author for use in a feed.
327 * @see get_comment_author()
329 * @param string $comment_author The current comment author.
331 return apply_filters( 'comment_author_rss', get_comment_author() );
335 * Display the current comment author in the feed.
339 function comment_author_rss() {
340 echo get_comment_author_rss();
344 * Display the current comment content for use in the feeds.
348 function comment_text_rss() {
349 $comment_text = get_comment_text();
351 * Filter the current comment content for use in a feed.
355 * @param string $comment_text The content of the current comment.
357 $comment_text = apply_filters( 'comment_text_rss', $comment_text );
362 * Retrieve all of the post categories, formatted for use in feeds.
364 * All of the categories for the current post in the feed loop, will be
365 * retrieved and have feed markup added, so that they can easily be added to the
366 * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
370 * @param string $type Optional, default is the type returned by get_default_feed().
371 * @return string All of the post categories for displaying in the feed.
373 function get_the_category_rss($type = null) {
375 $type = get_default_feed();
376 $categories = get_the_category();
377 $tags = get_the_tags();
379 $cat_names = array();
382 if ( 'atom' == $type )
385 if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
386 $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
389 if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
390 $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
393 $cat_names = array_unique($cat_names);
395 foreach ( $cat_names as $cat_name ) {
396 if ( 'rdf' == $type )
397 $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
398 elseif ( 'atom' == $type )
399 $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
401 $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
405 * Filter all of the post categories for display in a feed.
409 * @param string $the_list All of the RSS post categories.
410 * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
413 return apply_filters( 'the_category_rss', $the_list, $type );
417 * Display the post categories in the feed.
420 * @see get_the_category_rss() For better explanation.
422 * @param string $type Optional, default is the type returned by get_default_feed().
424 function the_category_rss($type = null) {
425 echo get_the_category_rss($type);
429 * Display the HTML type based on the blog setting.
431 * The two possible values are either 'xhtml' or 'html'.
435 function html_type_rss() {
436 $type = get_bloginfo('html_type');
437 if (strpos($type, 'xhtml') !== false)
445 * Display the rss enclosure for the current post.
447 * Uses the global $post to check whether the post requires a password and if
448 * the user has the password for the post. If not then it will return before
451 * Also uses the function get_post_custom() to get the post's 'enclosure'
452 * metadata field and parses the value to display the enclosure(s). The
453 * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
458 function rss_enclosure() {
459 if ( post_password_required() )
462 foreach ( (array) get_post_custom() as $key => $val) {
463 if ($key == 'enclosure') {
464 foreach ( (array) $val as $enc ) {
465 $enclosure = explode("\n", $enc);
467 // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
468 $t = preg_split('/[ \t]/', trim($enclosure[2]) );
472 * Filter the RSS enclosure HTML link tag for the current post.
476 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
478 echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
485 * Display the atom enclosure for the current post.
487 * Uses the global $post to check whether the post requires a password and if
488 * the user has the password for the post. If not then it will return before
491 * Also uses the function get_post_custom() to get the post's 'enclosure'
492 * metadata field and parses the value to display the enclosure(s). The
493 * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
497 function atom_enclosure() {
498 if ( post_password_required() )
501 foreach ( (array) get_post_custom() as $key => $val ) {
502 if ($key == 'enclosure') {
503 foreach ( (array) $val as $enc ) {
504 $enclosure = explode("\n", $enc);
506 * Filter the atom enclosure HTML link tag for the current post.
510 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
512 echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
519 * Determine the type of a string of data with the data formatted.
521 * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
523 * In the case of WordPress, text is defined as containing no markup,
524 * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
526 * Container div tags are added to xhtml values, per section 3.1.1.3.
528 * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
532 * @param string $data Input string
533 * @return array array(type, value)
535 function prep_atom_text_construct($data) {
536 if (strpos($data, '<') === false && strpos($data, '&') === false) {
537 return array('text', $data);
540 $parser = xml_parser_create();
541 xml_parse($parser, '<div>' . $data . '</div>', true);
542 $code = xml_get_error_code($parser);
543 xml_parser_free($parser);
546 if (strpos($data, '<') === false) {
547 return array('text', $data);
549 $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
550 return array('xhtml', $data);
554 if (strpos($data, ']]>') === false) {
555 return array('html', "<![CDATA[$data]]>");
557 return array('html', htmlspecialchars($data));
562 * Displays Site Icon in atom feeds.
566 * @see get_site_icon_url()
568 function atom_site_icon() {
569 $url = get_site_icon_url( 32 );
571 echo "<icon>$url</icon>\n";
576 * Displays Site Icon in RSS2.
580 function rss2_site_icon() {
581 $rss_title = get_wp_title_rss();
582 if ( empty( $rss_title ) ) {
583 $rss_title = get_bloginfo_rss( 'name' );
586 $url = get_site_icon_url( 32 );
590 <url>' . convert_chars( $url ) . '</url>
591 <title>' . $rss_title . '</title>
592 <link>' . get_bloginfo_rss( 'url' ) . '</link>
600 * Display the link for the currently displayed feed in a XSS safe way.
602 * Generate a correct link for the atom:self element.
606 function self_link() {
607 $host = @parse_url(home_url());
609 * Filter the current feed URL.
613 * @see set_url_scheme()
616 * @param string $feed_link The link for the feed with set URL scheme.
618 echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
622 * Return the content type for specified feed type.
626 * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
628 function feed_content_type( $type = '' ) {
630 $type = get_default_feed();
633 'rss' => 'application/rss+xml',
634 'rss2' => 'application/rss+xml',
635 'rss-http' => 'text/xml',
636 'atom' => 'application/atom+xml',
637 'rdf' => 'application/rdf+xml'
640 $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
643 * Filter the content type for a specific feed type.
647 * @param string $content_type Content type indicating the type of data that a feed contains.
648 * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
650 return apply_filters( 'feed_content_type', $content_type, $type );
654 * Build SimplePie object based on RSS or Atom feed from URL.
658 * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
659 * using SimplePie's multifeed feature.
660 * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
662 * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
664 function fetch_feed( $url ) {
665 require_once( ABSPATH . WPINC . '/class-feed.php' );
667 $feed = new SimplePie();
669 $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
670 // We must manually overwrite $feed->sanitize because SimplePie's
671 // constructor sets it before we have a chance to set the sanitization class
672 $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
674 $feed->set_cache_class( 'WP_Feed_Cache' );
675 $feed->set_file_class( 'WP_SimplePie_File' );
677 $feed->set_feed_url( $url );
678 /** This filter is documented in wp-includes/class-feed.php */
679 $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
681 * Fires just before processing the SimplePie feed object.
685 * @param object &$feed SimplePie feed object, passed by reference.
686 * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
688 do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
690 $feed->set_output_encoding( get_option( 'blog_charset' ) );
691 $feed->handle_content_type();
693 if ( $feed->error() )
694 return new WP_Error( 'simplepie-error', $feed->error() );