Using WordPress ‘commentrss2_item’ PHP action

The commentrss2_item WordPress PHP action fires at the end of each RSS2 comment feed item, allowing you to add custom content or modify the output of the comment feed.

Usage

add_action('commentrss2_item', 'my_custom_function', 10, 2);

function my_custom_function($comment_id, $comment_post_id) {
  // Your custom code here

}

Parameters

  • $comment_id (int) – The ID of the comment being displayed.
  • $comment_post_id (int) – The ID of the post the comment is connected to.

More information

See WordPress Developer Resources: commentrss2_item

Examples

Add a custom field to the comment RSS2 feed

This example adds a custom field value to the comment RSS2 feed.

add_action('commentrss2_item', 'add_custom_field_to_comment_feed', 10, 2);

function add_custom_field_to_comment_feed($comment_id, $comment_post_id) {
  $custom_field_value = get_comment_meta($comment_id, 'custom_field_key', true);
  if ($custom_field_value) {
    echo '<customField>' . esc_html($custom_field_value) . '</customField>';
  }
}

Add the comment author’s email to the feed

This example adds the comment author’s email to the comment RSS2 feed.

add_action('commentrss2_item', 'add_comment_author_email_to_feed', 10, 2);

function add_comment_author_email_to_feed($comment_id, $comment_post_id) {
  $comment = get_comment($comment_id);
  echo '<commentAuthorEmail>' . esc_html($comment->comment_author_email) . '</commentAuthorEmail>';
}

Add the comment author’s URL to the feed

This example adds the comment author’s URL to the comment RSS2 feed.

add_action('commentrss2_item', 'add_comment_author_url_to_feed', 10, 2);

function add_comment_author_url_to_feed($comment_id, $comment_post_id) {
  $comment = get_comment($comment_id);
  echo '<commentAuthorUrl>' . esc_url($comment->comment_author_url) . '</commentAuthorUrl>';
}

Add the comment’s parent ID to the feed

This example adds the parent comment ID to the comment RSS2 feed.

add_action('commentrss2_item', 'add_comment_parent_id_to_feed', 10, 2);

function add_comment_parent_id_to_feed($comment_id, $comment_post_id) {
  $comment = get_comment($comment_id);
  echo '<commentParentId>' . intval($comment->comment_parent) . '</commentParentId>';
}

Add a custom message to the feed

This example adds a custom message to the end of each comment in the RSS2 feed.

add_action('commentrss2_item', 'add_custom_message_to_feed', 10, 2);

function add_custom_message_to_feed($comment_id, $comment_post_id) {
  echo '<customMessage>Thanks for reading this comment!</customMessage>';
}