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.
74 * @uses apply_filters() Calls 'default_feed' hook on the default feed string.
76 * @return string Default feed, or for example 'rss2', 'atom', etc.
78 function get_default_feed() {
80 * Filter 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.
96 * @param string $sep Optional.How to separate the title. See wp_title() for more info.
97 * @return string Error message on failure or blog title on success.
99 function get_wp_title_rss($sep = '»') {
100 $title = wp_title($sep, false);
101 if ( is_wp_error( $title ) )
102 return $title->get_error_message();
104 * Filter the blog title for use as the feed title.
108 * @param string $title The current blog title.
109 * @param string $sep Separator used by wp_title().
111 $title = apply_filters( 'get_wp_title_rss', $title, $sep );
116 * Display the blog title for display of the feed title.
119 * @see wp_title() $sep parameter usage.
121 * @param string $sep Optional.
123 function wp_title_rss( $sep = '»' ) {
125 * Filter the blog title for display of the feed title.
129 * @see get_wp_title_rss()
131 * @param string $wp_title The current blog title.
132 * @param string $sep Separator used by wp_title().
134 echo apply_filters( 'wp_title_rss', get_wp_title_rss( $sep ), $sep );
138 * Retrieve the current post title for the feed.
142 * @return string Current post title.
144 function get_the_title_rss() {
145 $title = get_the_title();
147 * Filter the post title for use in a feed.
151 * @param string $title The current post title.
153 $title = apply_filters( 'the_title_rss', $title );
158 * Display the post title in the feed.
161 * @uses get_the_title_rss() Used to retrieve current post title.
163 function the_title_rss() {
164 echo get_the_title_rss();
168 * Retrieve the post content for feeds.
171 * @see get_the_content()
173 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
174 * @return string The filtered content.
176 function get_the_content_feed($feed_type = null) {
178 $feed_type = get_default_feed();
180 /** This filter is documented in wp-admin/post-template.php */
181 $content = apply_filters( 'the_content', get_the_content() );
182 $content = str_replace(']]>', ']]>', $content);
184 * Filter the post content for use in feeds.
188 * @param string $content The current post content.
189 * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
192 return apply_filters( 'the_content_feed', $content, $feed_type );
196 * Display the post content for feeds.
199 * @uses apply_filters() Calls 'the_content_feed' on the content before processing.
200 * @see get_the_content()
202 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
204 function the_content_feed($feed_type = null) {
205 echo get_the_content_feed($feed_type);
209 * Display the post excerpt for the feed.
213 function the_excerpt_rss() {
214 $output = get_the_excerpt();
216 * Filter the post excerpt for a feed.
220 * @param string $output The current post excerpt.
222 echo apply_filters( 'the_excerpt_rss', $output );
226 * Display the permalink to the post for use in feeds.
230 function the_permalink_rss() {
232 * Filter the permalink to the post for use in feeds.
236 * @param string $post_permalink The current post permalink.
238 echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
242 * Outputs the link to the comments for the current post in an xml safe way
247 function comments_link_feed() {
249 * Filter the comments permalink for the current post.
253 * @param string $comment_permalink The current comment permalink with
254 * '#comments' appended.
256 echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) );
260 * Display the feed GUID for the current comment.
264 * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
266 function comment_guid($comment_id = null) {
267 echo esc_url( get_comment_guid($comment_id) );
271 * Retrieve the feed GUID for the current comment.
275 * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
276 * @return bool|string false on failure or guid for comment on success.
278 function get_comment_guid($comment_id = null) {
279 $comment = get_comment($comment_id);
281 if ( !is_object($comment) )
284 return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
288 * Display the link to the comments.
292 function comment_link() {
294 * Filter the current comment's permalink.
298 * @see get_comment_link()
300 * @param string $comment_permalink The current comment permalink.
302 echo esc_url( apply_filters( 'comment_link', get_comment_link() ) );
306 * Retrieve the current comment author for use in the feeds.
309 * @uses get_comment_author()
311 * @return string Comment Author
313 function get_comment_author_rss() {
315 * Filter the current comment author for use in a feed.
319 * @see get_comment_author()
321 * @param string $comment_author The current comment author.
323 return apply_filters( 'comment_author_rss', get_comment_author() );
327 * Display the current comment author in the feed.
331 function comment_author_rss() {
332 echo get_comment_author_rss();
336 * Display the current comment content for use in the feeds.
339 * @uses get_comment_text()
341 function comment_text_rss() {
342 $comment_text = get_comment_text();
344 * Filter the current comment content for use in a feed.
348 * @param string $comment_text The content of the current comment.
350 $comment_text = apply_filters( 'comment_text_rss', $comment_text );
355 * Retrieve all of the post categories, formatted for use in feeds.
357 * All of the categories for the current post in the feed loop, will be
358 * retrieved and have feed markup added, so that they can easily be added to the
359 * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
363 * @param string $type Optional, default is the type returned by get_default_feed().
364 * @return string All of the post categories for displaying in the feed.
366 function get_the_category_rss($type = null) {
368 $type = get_default_feed();
369 $categories = get_the_category();
370 $tags = get_the_tags();
372 $cat_names = array();
375 if ( 'atom' == $type )
378 if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
379 $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
382 if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
383 $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
386 $cat_names = array_unique($cat_names);
388 foreach ( $cat_names as $cat_name ) {
389 if ( 'rdf' == $type )
390 $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
391 elseif ( 'atom' == $type )
392 /** This filter is documented in wp-includes/feed.php */
393 $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( apply_filters( 'get_bloginfo_rss', get_bloginfo( '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
451 * @uses get_post_custom() To get the current post enclosure metadata.
453 function rss_enclosure() {
454 if ( post_password_required() )
457 foreach ( (array) get_post_custom() as $key => $val) {
458 if ($key == 'enclosure') {
459 foreach ( (array) $val as $enc ) {
460 $enclosure = explode("\n", $enc);
462 // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
463 $t = preg_split('/[ \t]/', trim($enclosure[2]) );
467 * Filter the RSS enclosure HTML link tag for the current post.
471 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
473 echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
480 * Display the atom enclosure for the current post.
482 * Uses the global $post to check whether the post requires a password and if
483 * the user has the password for the post. If not then it will return before
486 * Also uses the function get_post_custom() to get the post's 'enclosure'
487 * metadata field and parses the value to display the enclosure(s). The
488 * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
491 * @uses get_post_custom() To get the current post enclosure metadata.
493 function atom_enclosure() {
494 if ( post_password_required() )
497 foreach ( (array) get_post_custom() as $key => $val ) {
498 if ($key == 'enclosure') {
499 foreach ( (array) $val as $enc ) {
500 $enclosure = explode("\n", $enc);
502 * Filter the atom enclosure HTML link tag for the current post.
506 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
508 echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
515 * Determine the type of a string of data with the data formatted.
517 * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
519 * In the case of WordPress, text is defined as containing no markup,
520 * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
522 * Container div tags are added to xhtml values, per section 3.1.1.3.
524 * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
528 * @param string $data Input string
529 * @return array array(type, value)
531 function prep_atom_text_construct($data) {
532 if (strpos($data, '<') === false && strpos($data, '&') === false) {
533 return array('text', $data);
536 $parser = xml_parser_create();
537 xml_parse($parser, '<div>' . $data . '</div>', true);
538 $code = xml_get_error_code($parser);
539 xml_parser_free($parser);
542 if (strpos($data, '<') === false) {
543 return array('text', $data);
545 $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
546 return array('xhtml', $data);
550 if (strpos($data, ']]>') == false) {
551 return array('html', "<![CDATA[$data]]>");
553 return array('html', htmlspecialchars($data));
558 * Display the link for the currently displayed feed in a XSS safe way.
560 * Generate a correct link for the atom:self element.
564 function self_link() {
565 $host = @parse_url(home_url());
567 * Filter the current feed URL.
571 * @see set_url_scheme()
574 * @param string $feed_link The link for the feed with set URL scheme.
576 echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
580 * Return the content type for specified feed type.
584 function feed_content_type( $type = '' ) {
586 $type = get_default_feed();
589 'rss' => 'application/rss+xml',
590 'rss2' => 'application/rss+xml',
591 'rss-http' => 'text/xml',
592 'atom' => 'application/atom+xml',
593 'rdf' => 'application/rdf+xml'
596 $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
599 * Filter the content type for a specific feed type.
603 * @param string $content_type Content type indicating the type of data that a feed contains.
604 * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
607 return apply_filters( 'feed_content_type', $content_type, $type );
611 * Build SimplePie object based on RSS or Atom feed from URL.
615 * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
616 * using SimplePie's multifeed feature.
617 * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
619 * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
621 function fetch_feed( $url ) {
622 require_once( ABSPATH . WPINC . '/class-feed.php' );
624 $feed = new SimplePie();
626 $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
627 // We must manually overwrite $feed->sanitize because SimplePie's
628 // constructor sets it before we have a chance to set the sanitization class
629 $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
631 $feed->set_cache_class( 'WP_Feed_Cache' );
632 $feed->set_file_class( 'WP_SimplePie_File' );
634 $feed->set_feed_url( $url );
635 /** This filter is documented in wp-includes/class-feed.php */
636 $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
638 * Fires just before processing the SimplePie feed object.
642 * @param object &$feed SimplePie feed object, passed by reference.
643 * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
645 do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
647 $feed->handle_content_type();
649 if ( $feed->error() )
650 return new WP_Error( 'simplepie-error', $feed->error() );