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.
95 * @param string $sep Optional. How to separate the title. See wp_title() for more info.
96 * @return string Error message on failure or blog title on success.
98 function get_wp_title_rss( $sep = '»' ) {
99 $title = wp_title( $sep, false );
101 if ( is_wp_error( $title ) ) {
102 return $title->get_error_message();
105 if ( $title && $sep && ' ' !== substr( $title, 0, 1 ) ) {
106 $title = " $sep " . $title;
110 * Filter the blog title for use as the feed title.
114 * @param string $title The current blog title.
115 * @param string $sep Separator used by wp_title().
117 $title = apply_filters( 'get_wp_title_rss', $title, $sep );
122 * Display the blog title for display of the feed title.
125 * @see wp_title() $sep parameter usage.
127 * @param string $sep Optional.
129 function wp_title_rss( $sep = '»' ) {
131 * Filter the blog title for display of the feed title.
135 * @see get_wp_title_rss()
137 * @param string $wp_title The current blog title.
138 * @param string $sep Separator used by wp_title().
140 echo apply_filters( 'wp_title_rss', get_wp_title_rss( $sep ), $sep );
144 * Retrieve the current post title for the feed.
148 * @return string Current post title.
150 function get_the_title_rss() {
151 $title = get_the_title();
153 * Filter the post title for use in a feed.
157 * @param string $title The current post title.
159 $title = apply_filters( 'the_title_rss', $title );
164 * Display the post title in the feed.
168 function the_title_rss() {
169 echo get_the_title_rss();
173 * Retrieve the post content for feeds.
176 * @see get_the_content()
178 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
179 * @return string The filtered content.
181 function get_the_content_feed($feed_type = null) {
183 $feed_type = get_default_feed();
185 /** This filter is documented in wp-includes/post-template.php */
186 $content = apply_filters( 'the_content', get_the_content() );
187 $content = str_replace(']]>', ']]>', $content);
189 * Filter the post content for use in feeds.
193 * @param string $content The current post content.
194 * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
197 return apply_filters( 'the_content_feed', $content, $feed_type );
201 * Display the post content for feeds.
205 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
207 function the_content_feed($feed_type = null) {
208 echo get_the_content_feed($feed_type);
212 * Display the post excerpt for the feed.
216 function the_excerpt_rss() {
217 $output = get_the_excerpt();
219 * Filter the post excerpt for a feed.
223 * @param string $output The current post excerpt.
225 echo apply_filters( 'the_excerpt_rss', $output );
229 * Display the permalink to the post for use in feeds.
233 function the_permalink_rss() {
235 * Filter the permalink to the post for use in feeds.
239 * @param string $post_permalink The current post permalink.
241 echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
245 * Outputs the link to the comments for the current post in an xml safe way
250 function comments_link_feed() {
252 * Filter the comments permalink for the current post.
256 * @param string $comment_permalink The current comment permalink with
257 * '#comments' appended.
259 echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) );
263 * Display the feed GUID for the current comment.
267 * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
269 function comment_guid($comment_id = null) {
270 echo esc_url( get_comment_guid($comment_id) );
274 * Retrieve the feed GUID for the current comment.
278 * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
279 * @return false|string false on failure or guid for comment on success.
281 function get_comment_guid($comment_id = null) {
282 $comment = get_comment($comment_id);
284 if ( !is_object($comment) )
287 return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
291 * Display the link to the comments.
295 function comment_link() {
297 * Filter the current comment's permalink.
301 * @see get_comment_link()
303 * @param string $comment_permalink The current comment permalink.
305 echo esc_url( apply_filters( 'comment_link', get_comment_link() ) );
309 * Retrieve the current comment author for use in the feeds.
313 * @return string Comment Author
315 function get_comment_author_rss() {
317 * Filter the current comment author for use in a feed.
321 * @see get_comment_author()
323 * @param string $comment_author The current comment author.
325 return apply_filters( 'comment_author_rss', get_comment_author() );
329 * Display the current comment author in the feed.
333 function comment_author_rss() {
334 echo get_comment_author_rss();
338 * Display the current comment content for use in the feeds.
342 function comment_text_rss() {
343 $comment_text = get_comment_text();
345 * Filter the current comment content for use in a feed.
349 * @param string $comment_text The content of the current comment.
351 $comment_text = apply_filters( 'comment_text_rss', $comment_text );
356 * Retrieve all of the post categories, formatted for use in feeds.
358 * All of the categories for the current post in the feed loop, will be
359 * retrieved and have feed markup added, so that they can easily be added to the
360 * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
364 * @param string $type Optional, default is the type returned by get_default_feed().
365 * @return string All of the post categories for displaying in the feed.
367 function get_the_category_rss($type = null) {
369 $type = get_default_feed();
370 $categories = get_the_category();
371 $tags = get_the_tags();
373 $cat_names = array();
376 if ( 'atom' == $type )
379 if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
380 $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
383 if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
384 $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
387 $cat_names = array_unique($cat_names);
389 foreach ( $cat_names as $cat_name ) {
390 if ( 'rdf' == $type )
391 $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
392 elseif ( 'atom' == $type )
393 $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
395 $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
399 * Filter all of the post categories for display in a feed.
403 * @param string $the_list All of the RSS post categories.
404 * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
407 return apply_filters( 'the_category_rss', $the_list, $type );
411 * Display the post categories in the feed.
414 * @see get_the_category_rss() For better explanation.
416 * @param string $type Optional, default is the type returned by get_default_feed().
418 function the_category_rss($type = null) {
419 echo get_the_category_rss($type);
423 * Display the HTML type based on the blog setting.
425 * The two possible values are either 'xhtml' or 'html'.
429 function html_type_rss() {
430 $type = get_bloginfo('html_type');
431 if (strpos($type, 'xhtml') !== false)
439 * Display the rss enclosure for the current post.
441 * Uses the global $post to check whether the post requires a password and if
442 * the user has the password for the post. If not then it will return before
445 * Also uses the function get_post_custom() to get the post's 'enclosure'
446 * metadata field and parses the value to display the enclosure(s). The
447 * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
452 function rss_enclosure() {
453 if ( post_password_required() )
456 foreach ( (array) get_post_custom() as $key => $val) {
457 if ($key == 'enclosure') {
458 foreach ( (array) $val as $enc ) {
459 $enclosure = explode("\n", $enc);
461 // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
462 $t = preg_split('/[ \t]/', trim($enclosure[2]) );
466 * Filter the RSS enclosure HTML link tag for the current post.
470 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
472 echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
479 * Display the atom enclosure for the current post.
481 * Uses the global $post to check whether the post requires a password and if
482 * the user has the password for the post. If not then it will return before
485 * Also uses the function get_post_custom() to get the post's 'enclosure'
486 * metadata field and parses the value to display the enclosure(s). The
487 * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
491 function atom_enclosure() {
492 if ( post_password_required() )
495 foreach ( (array) get_post_custom() as $key => $val ) {
496 if ($key == 'enclosure') {
497 foreach ( (array) $val as $enc ) {
498 $enclosure = explode("\n", $enc);
500 * Filter the atom enclosure HTML link tag for the current post.
504 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
506 echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
513 * Determine the type of a string of data with the data formatted.
515 * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
517 * In the case of WordPress, text is defined as containing no markup,
518 * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
520 * Container div tags are added to xhtml values, per section 3.1.1.3.
522 * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
526 * @param string $data Input string
527 * @return array array(type, value)
529 function prep_atom_text_construct($data) {
530 if (strpos($data, '<') === false && strpos($data, '&') === false) {
531 return array('text', $data);
534 $parser = xml_parser_create();
535 xml_parse($parser, '<div>' . $data . '</div>', true);
536 $code = xml_get_error_code($parser);
537 xml_parser_free($parser);
540 if (strpos($data, '<') === false) {
541 return array('text', $data);
543 $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
544 return array('xhtml', $data);
548 if (strpos($data, ']]>') === false) {
549 return array('html', "<![CDATA[$data]]>");
551 return array('html', htmlspecialchars($data));
556 * Displays Site Icon in atom feeds.
560 * @see get_site_icon_url()
562 function atom_site_icon() {
563 $url = get_site_icon_url( 32 );
565 echo "<icon>$url</icon>\n";
570 * Displays Site Icon in RSS2.
574 function rss2_site_icon() {
575 $rss_title = get_wp_title_rss();
576 if ( empty( $rss_title ) ) {
577 $rss_title = get_bloginfo_rss( 'name' );
580 $url = get_site_icon_url( 32 );
584 <url>' . convert_chars( $url ) . '</url>
585 <title>' . $rss_title . '</title>
586 <link>' . get_bloginfo_rss( 'url' ) . '</link>
594 * Display the link for the currently displayed feed in a XSS safe way.
596 * Generate a correct link for the atom:self element.
600 function self_link() {
601 $host = @parse_url(home_url());
603 * Filter the current feed URL.
607 * @see set_url_scheme()
610 * @param string $feed_link The link for the feed with set URL scheme.
612 echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
616 * Return the content type for specified feed type.
620 function feed_content_type( $type = '' ) {
622 $type = get_default_feed();
625 'rss' => 'application/rss+xml',
626 'rss2' => 'application/rss+xml',
627 'rss-http' => 'text/xml',
628 'atom' => 'application/atom+xml',
629 'rdf' => 'application/rdf+xml'
632 $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
635 * Filter the content type for a specific feed type.
639 * @param string $content_type Content type indicating the type of data that a feed contains.
640 * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
643 return apply_filters( 'feed_content_type', $content_type, $type );
647 * Build SimplePie object based on RSS or Atom feed from URL.
651 * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
652 * using SimplePie's multifeed feature.
653 * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
655 * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
657 function fetch_feed( $url ) {
658 require_once( ABSPATH . WPINC . '/class-feed.php' );
660 $feed = new SimplePie();
662 $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
663 // We must manually overwrite $feed->sanitize because SimplePie's
664 // constructor sets it before we have a chance to set the sanitization class
665 $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
667 $feed->set_cache_class( 'WP_Feed_Cache' );
668 $feed->set_file_class( 'WP_SimplePie_File' );
670 $feed->set_feed_url( $url );
671 /** This filter is documented in wp-includes/class-feed.php */
672 $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
674 * Fires just before processing the SimplePie feed object.
678 * @param object &$feed SimplePie feed object, passed by reference.
679 * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
681 do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
683 $feed->set_output_encoding( get_option( 'blog_charset' ) );
684 $feed->handle_content_type();
686 if ( $feed->error() )
687 return new WP_Error( 'simplepie-error', $feed->error() );