TechJunkie is a BOX20 Media Company

Home Cut the Cord Plex Creating Custom WordPress RSS/XML Feed

Creating Custom WordPress RSS/XML Feed

You at some point in your WordPress development may need to provide someone with a custom feed. Whether that’s to provide someone an API, or just provide a better experience for a certain set of users, it is easily done.

I prefer to create a new feed rather than extend the default feeds as I find this method a bit simpler

add_feed WordPress function


function tj_init_custom_feed() { //initialize the feed

In your functions.php file in your WordPress theme, add the code above. As its best not to call add_feed directly, we add it through a filter on ‘init’. The first parameter in the function call is used to provide the URL slug for the feed. The second parameter is used to tie it to a function name. So, when that url is called(, it executes the PHP function tj_custom_feed.

Please note that the rewrite rules for WordPress must be flushed before that URL will be recognized properly. A good simple way to force the rules to be flushed is to go to the WordPress Admin -> Settings -> Permalinks, and then click the save changes button.

Outputting the XML

There is really nothing too complex about outputting the RSS/XML feed code. First, the content-type is set via the php header function so it can be rendered appropriately. Next, we retrieve some data from get_posts, loop through it, and echo it out to the screen.

function tj_custom_feed() {
  header("Content-type: text/xml");

  echo "\n";
  echo "\n";

  $posts = get_posts();

  foreach($posts as $post){
    $post_link = get_permalink( $post->ID );
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );

    echo '';
    echo "\t" . $post->ID . "\n";

    echo "\t" . $post->post_date . "\n";
    echo "\t" . $post_link . "\n";
    echo "\t" . esc_html($post->post_title) . "\n";
    echo "\t" . esc_html(strip_tags($post->post_excerpt)) . "\n";
    echo "\t" . $image[0] . "";
    echo '';

  echo "";

How To Stream Video From A Camera With No Firewire Port

Read Next 

One thought on “Creating Custom WordPress RSS/XML Feed”

Jeffrey Fry says:
This is great.. but what if you want an xml feed for certain pages on your site?

Leave a Reply

Your email address will not be published. Required fields are marked *


Nov 15, 2012

57 Articles Published