Using WordPress ‘rdf_item’ PHP action

The rdf_item WordPress PHP action fires at the end of each RDF feed item.

Usage

add_action('rdf_item', 'your_custom_function');
function your_custom_function() {
// your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: rdf_item

Examples

Add a custom field to RDF feed items

This example adds a custom field called “author_bio” to each RDF feed item.

add_action('rdf_item', 'add_author_bio_to_rdf');

function add_author_bio_to_rdf() {
  global $post;
  $author_bio = get_post_meta($post->ID, 'author_bio', true);
  if (!empty($author_bio)) {
    echo "<author_bio>" . esc_html($author_bio) . "</author_bio>";
  }
}

Add post tags to RDF feed items

This example adds the post tags to each RDF feed item as a comma-separated list.

add_action('rdf_item', 'add_tags_to_rdf');

function add_tags_to_rdf() {
  global $post;
  $tags = get_the_tags($post->ID);
  if ($tags) {
    $tag_list = [];
    foreach ($tags as $tag) {
      $tag_list[] = $tag->name;
    }
    echo "<post_tags>" . implode(', ', $tag_list) . "</post_tags>";
  }
}

This example adds the featured image URL to each RDF feed item.

add_action('rdf_item', 'add_featured_image_to_rdf');

function add_featured_image_to_rdf() {
  global $post;
  if (has_post_thumbnail($post->ID)) {
    $image_url = get_the_post_thumbnail_url($post->ID, 'full');
    echo "<featured_image_url>" . esc_url($image_url) . "</featured_image_url>";
  }
}

This example adds a custom copyright notice to each RDF feed item.

add_action('rdf_item', 'add_copyright_to_rdf');

function add_copyright_to_rdf() {
  global $post;
  $copyright_notice = "Copyright " . date('Y') . " " . get_bloginfo('name');
  echo "<copyright_notice>" . esc_html($copyright_notice) . "</copyright_notice>";
}

Add an audio file URL from a custom field to RDF feed items

This example adds an audio file URL from a custom field called “audio_file” to each RDF feed item.

add_action('rdf_item', 'add_audio_file_to_rdf');

function add_audio_file_to_rdf() {
  global $post;
  $audio_file = get_post_meta($post->ID, 'audio_file', true);
  if (!empty($audio_file)) {
    echo "<audio_file_url>" . esc_url($audio_file) . "</audio_file_url>";
  }
}