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.
22 * @uses apply_filters() Calls 'get_bloginfo_rss' hook with two parameters.
23 * @see get_bloginfo() For the list of possible values to display.
25 * @param string $show See get_bloginfo() for possible values.
28 function get_bloginfo_rss($show = '') {
29 $info = strip_tags(get_bloginfo($show));
30 return apply_filters('get_bloginfo_rss', convert_chars($info), $show);
34 * Display RSS container for the bloginfo function.
36 * You can retrieve anything that you can using the get_bloginfo() function.
37 * Everything will be stripped of tags and characters converted, when the values
38 * are retrieved for use in the feeds.
43 * @uses apply_filters() Calls 'bloginfo_rss' hook with two parameters.
44 * @see get_bloginfo() For the list of possible values to display.
46 * @param string $show See get_bloginfo() for possible values.
48 function bloginfo_rss($show = '') {
49 echo apply_filters('bloginfo_rss', get_bloginfo_rss($show), $show);
53 * Retrieve the default feed.
55 * The default feed is 'rss2', unless a plugin changes it through the
56 * 'default_feed' filter.
61 * @uses apply_filters() Calls 'default_feed' hook on the default feed string.
63 * @return string Default feed, or for example 'rss2', 'atom', etc.
65 function get_default_feed() {
66 return apply_filters('default_feed', 'rss2');
70 * Retrieve the blog title for the feed title.
75 * @uses apply_filters() Calls 'get_wp_title_rss' hook on title.
76 * @uses wp_title() See function for $sep parameter usage.
78 * @param string $sep Optional.How to separate the title. See wp_title() for more info.
79 * @return string Error message on failure or blog title on success.
81 function get_wp_title_rss($sep = '»') {
82 $title = wp_title($sep, false);
83 if ( is_wp_error( $title ) )
84 return $title->get_error_message();
85 $title = apply_filters('get_wp_title_rss', $title);
90 * Display the blog title for display of the feed title.
95 * @uses apply_filters() Calls 'wp_title_rss' on the blog title.
96 * @see wp_title() $sep parameter usage.
98 * @param string $sep Optional.
100 function wp_title_rss($sep = '»') {
101 echo apply_filters('wp_title_rss', get_wp_title_rss($sep));
105 * Retrieve the current post title for the feed.
110 * @uses apply_filters() Calls 'the_title_rss' on the post title.
112 * @return string Current post title.
114 function get_the_title_rss() {
115 $title = get_the_title();
116 $title = apply_filters('the_title_rss', $title);
121 * Display the post title in the feed.
126 * @uses get_the_title_rss() Used to retrieve current post title.
128 function the_title_rss() {
129 echo get_the_title_rss();
133 * Retrieve the post content for feeds.
138 * @uses apply_filters() Calls 'the_content_feed' on the content before processing.
139 * @see get_the_content()
141 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
143 function get_the_content_feed($feed_type = null) {
145 $feed_type = get_default_feed();
147 $content = apply_filters('the_content', get_the_content());
148 $content = str_replace(']]>', ']]>', $content);
149 return apply_filters('the_content_feed', $content, $feed_type);
153 * Display the post content for feeds.
158 * @uses apply_filters() Calls 'the_content_feed' on the content before processing.
159 * @see get_the_content()
161 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
163 function the_content_feed($feed_type = null) {
164 echo get_the_content_feed($feed_type);
168 * Display the post excerpt for the feed.
173 * @uses apply_filters() Calls 'the_excerpt_rss' hook on the excerpt.
175 function the_excerpt_rss() {
176 $output = get_the_excerpt();
177 echo apply_filters('the_excerpt_rss', $output);
181 * Display the permalink to the post for use in feeds.
186 * @uses apply_filters() Call 'the_permalink_rss' on the post permalink
188 function the_permalink_rss() {
189 echo esc_url( apply_filters('the_permalink_rss', get_permalink() ));
193 * Outputs the link to the comments for the current post in an xml safe way
198 function comments_link_feed() {
199 echo esc_url( get_comments_link() );
203 * Display the feed GUID for the current comment.
209 * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
211 function comment_guid($comment_id = null) {
212 echo esc_url( get_comment_guid($comment_id) );
216 * Retrieve the feed GUID for the current comment.
222 * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
223 * @return bool|string false on failure or guid for comment on success.
225 function get_comment_guid($comment_id = null) {
226 $comment = get_comment($comment_id);
228 if ( !is_object($comment) )
231 return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
235 * Display the link to the comments.
239 function comment_link() {
240 echo esc_url( get_comment_link() );
244 * Retrieve the current comment author for use in the feeds.
249 * @uses apply_filters() Calls 'comment_author_rss' hook on comment author.
250 * @uses get_comment_author()
252 * @return string Comment Author
254 function get_comment_author_rss() {
255 return apply_filters('comment_author_rss', get_comment_author() );
259 * Display the current comment author in the feed.
265 function comment_author_rss() {
266 echo get_comment_author_rss();
270 * Display the current comment content for use in the feeds.
275 * @uses apply_filters() Calls 'comment_text_rss' filter on comment content.
276 * @uses get_comment_text()
278 function comment_text_rss() {
279 $comment_text = get_comment_text();
280 $comment_text = apply_filters('comment_text_rss', $comment_text);
285 * Retrieve all of the post categories, formatted for use in feeds.
287 * All of the categories for the current post in the feed loop, will be
288 * retrieved and have feed markup added, so that they can easily be added to the
289 * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
294 * @uses apply_filters()
296 * @param string $type Optional, default is the type returned by get_default_feed().
297 * @return string All of the post categories for displaying in the feed.
299 function get_the_category_rss($type = null) {
301 $type = get_default_feed();
302 $categories = get_the_category();
303 $tags = get_the_tags();
305 $cat_names = array();
308 if ( 'atom' == $type )
311 if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
312 $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
315 if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
316 $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
319 $cat_names = array_unique($cat_names);
321 foreach ( $cat_names as $cat_name ) {
322 if ( 'rdf' == $type )
323 $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
324 elseif ( 'atom' == $type )
325 $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( apply_filters( 'get_bloginfo_rss', get_bloginfo( 'url' ) ) ), esc_attr( $cat_name ) );
327 $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
330 return apply_filters('the_category_rss', $the_list, $type);
334 * Display the post categories in the feed.
339 * @see get_the_category_rss() For better explanation.
341 * @param string $type Optional, default is the type returned by get_default_feed().
343 function the_category_rss($type = null) {
344 echo get_the_category_rss($type);
348 * Display the HTML type based on the blog setting.
350 * The two possible values are either 'xhtml' or 'html'.
356 function html_type_rss() {
357 $type = get_bloginfo('html_type');
358 if (strpos($type, 'xhtml') !== false)
366 * Display the rss enclosure for the current post.
368 * Uses the global $post to check whether the post requires a password and if
369 * the user has the password for the post. If not then it will return before
372 * Also uses the function get_post_custom() to get the post's 'enclosure'
373 * metadata field and parses the value to display the enclosure(s). The
374 * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
378 * @subpackage Template
380 * @uses apply_filters() Calls 'rss_enclosure' hook on rss enclosure.
381 * @uses get_post_custom() To get the current post enclosure metadata.
383 function rss_enclosure() {
384 if ( post_password_required() )
387 foreach ( (array) get_post_custom() as $key => $val) {
388 if ($key == 'enclosure') {
389 foreach ( (array) $val as $enc ) {
390 $enclosure = explode("\n", $enc);
392 //only get the the first element eg, audio/mpeg from 'audio/mpeg mpga mp2 mp3'
393 $t = preg_split('/[ \t]/', trim($enclosure[2]) );
396 echo apply_filters('rss_enclosure', '<enclosure url="' . trim(htmlspecialchars($enclosure[0])) . '" length="' . trim($enclosure[1]) . '" type="' . $type . '" />' . "\n");
403 * Display the atom enclosure for the current post.
405 * Uses the global $post to check whether the post requires a password and if
406 * the user has the password for the post. If not then it will return before
409 * Also uses the function get_post_custom() to get the post's 'enclosure'
410 * metadata field and parses the value to display the enclosure(s). The
411 * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
414 * @subpackage Template
416 * @uses apply_filters() Calls 'atom_enclosure' hook on atom enclosure.
417 * @uses get_post_custom() To get the current post enclosure metadata.
419 function atom_enclosure() {
420 if ( post_password_required() )
423 foreach ( (array) get_post_custom() as $key => $val ) {
424 if ($key == 'enclosure') {
425 foreach ( (array) $val as $enc ) {
426 $enclosure = split("\n", $enc);
427 echo apply_filters('atom_enclosure', '<link href="' . trim(htmlspecialchars($enclosure[0])) . '" rel="enclosure" length="' . trim($enclosure[1]) . '" type="' . trim($enclosure[2]) . '" />' . "\n");
434 * Determine the type of a string of data with the data formatted.
436 * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
438 * In the case of WordPress, text is defined as containing no markup,
439 * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
441 * Container div tags are added to xhtml values, per section 3.1.1.3.
443 * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
449 * @param string $data Input string
450 * @return array array(type, value)
452 function prep_atom_text_construct($data) {
453 if (strpos($data, '<') === false && strpos($data, '&') === false) {
454 return array('text', $data);
457 $parser = xml_parser_create();
458 xml_parse($parser, '<div>' . $data . '</div>', true);
459 $code = xml_get_error_code($parser);
460 xml_parser_free($parser);
463 if (strpos($data, '<') === false) {
464 return array('text', $data);
466 $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
467 return array('xhtml', $data);
471 if (strpos($data, ']]>') == false) {
472 return array('html', "<![CDATA[$data]]>");
474 return array('html', htmlspecialchars($data));
479 * Display the link for the currently displayed feed in a XSS safe way.
481 * Generate a correct link for the atom:self element.
487 function self_link() {
488 $host = @parse_url(home_url());
489 $host = $host['host'];
492 . ( (isset($_SERVER['https']) && $_SERVER['https'] == 'on') ? 's' : '' ) . '://'
494 . stripslashes($_SERVER['REQUEST_URI'])
499 * Return the content type for specified feed type.
505 function feed_content_type( $type = '' ) {
507 $type = get_default_feed();
510 'rss' => 'application/rss+xml',
511 'rss2' => 'application/rss+xml',
512 'rss-http' => 'text/xml',
513 'atom' => 'application/atom+xml',
514 'rdf' => 'application/rdf+xml'
517 $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
519 return apply_filters( 'feed_content_type', $content_type, $type );
523 * Build SimplePie object based on RSS or Atom feed from URL.
527 * @param string $url URL to retrieve feed
528 * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
530 function fetch_feed($url) {
531 require_once (ABSPATH . WPINC . '/class-feed.php');
533 $feed = new SimplePie();
534 $feed->set_feed_url($url);
535 $feed->set_cache_class('WP_Feed_Cache');
536 $feed->set_file_class('WP_SimplePie_File');
537 $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 43200, $url));
538 do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
540 $feed->handle_content_type();
542 if ( $feed->error() )
543 return new WP_Error('simplepie-error', $feed->error());