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 );
102
if ( is_wp_error( $title ) ) {
103
return $title->get_error_message();
106
if ( $title && $sep && ' ' !== substr( $title, 0, 1 ) ) {
107
$title = " $sep " . $title;
111
* Filter the blog title for use as the feed title.
115
* @param string $title The current blog title.
116
* @param string $sep Separator used by wp_title().
118
$title = apply_filters( 'get_wp_title_rss', $title, $sep );
123
* Display the blog title for display of the feed title.
126
* @see wp_title() $sep parameter usage.
128
* @param string $sep Optional.
130
function wp_title_rss( $sep = '»' ) {
132
* Filter the blog title for display of the feed title.
136
* @see get_wp_title_rss()
138
* @param string $wp_title The current blog title.
139
* @param string $sep Separator used by wp_title().
141
echo apply_filters( 'wp_title_rss', get_wp_title_rss( $sep ), $sep );
145
* Retrieve the current post title for the feed.
149
* @return string Current post title.
151
function get_the_title_rss() {
152
$title = get_the_title();
154
* Filter the post title for use in a feed.
158
* @param string $title The current post title.
160
$title = apply_filters( 'the_title_rss', $title );
165
* Display the post title in the feed.
168
* @uses get_the_title_rss() Used to retrieve current post title.
170
function the_title_rss() {
171
echo get_the_title_rss();
175
* Retrieve the post content for feeds.
178
* @see get_the_content()
180
* @param string $feed_type The type of feed. rss2 | atom | rss | rdf
181
* @return string The filtered content.
183
function get_the_content_feed($feed_type = null) {
185
$feed_type = get_default_feed();
187
/** This filter is documented in wp-includes/post-template.php */
188
$content = apply_filters( 'the_content', get_the_content() );
189
$content = str_replace(']]>', ']]>', $content);
191
* Filter the post content for use in feeds.
195
* @param string $content The current post content.
196
* @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
199
return apply_filters( 'the_content_feed', $content, $feed_type );
203
* Display the post content for feeds.
206
* @uses apply_filters() Calls 'the_content_feed' on the content before processing.
207
* @see get_the_content()
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
* Filter 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
* Filter 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
* Filter 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|object $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|object $comment_id Optional comment object or id. Defaults to global comment object.
283
* @return bool|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.
299
function comment_link() {
301
* Filter the current comment's permalink.
305
* @see get_comment_link()
307
* @param string $comment_permalink The current comment permalink.
309
echo esc_url( apply_filters( 'comment_link', get_comment_link() ) );
313
* Retrieve the current comment author for use in the feeds.
316
* @uses get_comment_author()
318
* @return string Comment Author
320
function get_comment_author_rss() {
322
* Filter the current comment author for use in a feed.
326
* @see get_comment_author()
328
* @param string $comment_author The current comment author.
330
return apply_filters( 'comment_author_rss', get_comment_author() );
334
* Display the current comment author in the feed.
338
function comment_author_rss() {
339
echo get_comment_author_rss();
343
* Display the current comment content for use in the feeds.
346
* @uses get_comment_text()
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
457
* @uses get_post_custom() To get the current post enclosure metadata.
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
* Filter 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.
497
* @uses get_post_custom() To get the current post enclosure metadata.
499
function atom_enclosure() {
500
if ( post_password_required() )
503
foreach ( (array) get_post_custom() as $key => $val ) {
504
if ($key == 'enclosure') {
505
foreach ( (array) $val as $enc ) {
506
$enclosure = explode("\n", $enc);
508
* Filter the atom enclosure HTML link tag for the current post.
512
* @param string $html_link_tag The HTML link tag with a URI and other attributes.
514
echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
521
* Determine the type of a string of data with the data formatted.
523
* Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
525
* In the case of WordPress, text is defined as containing no markup,
526
* xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
528
* Container div tags are added to xhtml values, per section 3.1.1.3.
530
* @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
534
* @param string $data Input string
535
* @return array array(type, value)
537
function prep_atom_text_construct($data) {
538
if (strpos($data, '<') === false && strpos($data, '&') === false) {
539
return array('text', $data);
542
$parser = xml_parser_create();
543
xml_parse($parser, '<div>' . $data . '</div>', true);
544
$code = xml_get_error_code($parser);
545
xml_parser_free($parser);
548
if (strpos($data, '<') === false) {
549
return array('text', $data);
551
$data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
552
return array('xhtml', $data);
556
if (strpos($data, ']]>') == false) {
557
return array('html', "<![CDATA[$data]]>");
559
return array('html', htmlspecialchars($data));
564
* Display the link for the currently displayed feed in a XSS safe way.
566
* Generate a correct link for the atom:self element.
570
function self_link() {
571
$host = @parse_url(home_url());
573
* Filter the current feed URL.
577
* @see set_url_scheme()
580
* @param string $feed_link The link for the feed with set URL scheme.
582
echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
586
* Return the content type for specified feed type.
590
function feed_content_type( $type = '' ) {
592
$type = get_default_feed();
595
'rss' => 'application/rss+xml',
596
'rss2' => 'application/rss+xml',
597
'rss-http' => 'text/xml',
598
'atom' => 'application/atom+xml',
599
'rdf' => 'application/rdf+xml'
602
$content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
605
* Filter the content type for a specific feed type.
609
* @param string $content_type Content type indicating the type of data that a feed contains.
610
* @param string $type Type of feed. Possible values include 'rss2', 'atom'.
613
return apply_filters( 'feed_content_type', $content_type, $type );
617
* Build SimplePie object based on RSS or Atom feed from URL.
621
* @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
622
* using SimplePie's multifeed feature.
623
* See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
625
* @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
627
function fetch_feed( $url ) {
628
require_once( ABSPATH . WPINC . '/class-feed.php' );
630
$feed = new SimplePie();
632
$feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
633
// We must manually overwrite $feed->sanitize because SimplePie's
634
// constructor sets it before we have a chance to set the sanitization class
635
$feed->sanitize = new WP_SimplePie_Sanitize_KSES();
637
$feed->set_cache_class( 'WP_Feed_Cache' );
638
$feed->set_file_class( 'WP_SimplePie_File' );
640
$feed->set_feed_url( $url );
641
/** This filter is documented in wp-includes/class-feed.php */
642
$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
644
* Fires just before processing the SimplePie feed object.
648
* @param object &$feed SimplePie feed object, passed by reference.
649
* @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
651
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
653
$feed->handle_content_type();
655
if ( $feed->error() )
656
return new WP_Error( 'simplepie-error', $feed->error() );