Welcome to WebmasterWorld Guest from

Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

How to save WordPress RSS Feed to a File?

5:10 pm on Mar 4, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 2, 2012
votes: 0

As per the topic title, I actually want to do two things but we'll start with the first. Essentially I want to save my RSS feed output..


(I also tried with the following url - both work when you use them in a browser)


..to a static file like feed.xml or feed.txt. I tried CURL, which works with all my other PHP systems for similar tasks, but something about WP prevents it from working.

function get_data($url)
$ch = curl_init();
$timeout = 5;
$data = curl_exec($ch);
return $data;

$returned_content = get_data('http://www.mysite.co.uk/index.php?feed=rss');

echo $returned_content;

$fp = fopen("feed.txt", "w");
fwrite($fp, $returned_content);

Likewise when I try to rewrite (htaccess) any WP URL (this happens regardless of whether I use WP's pathinfo or mod_rewrite solution for pretty URLs) to another, such as telling requests for "/feed.xml" to pull their data from "/index.php/feed" .. it also fails but produces a 404 (note that WP's feed links do actually work).

I think I tried some like this:

RewriteRule ^meow\.xml$ index\.php?feed=rss [QSA,L]

and also tried

RewriteRule ^meow.xml(/?)+$ index.php?feed=rss [QSA,L]

No dice. I don't have any WP code in my ROOT htaccess because I'm using Pathinfo.

Any ideas on either or those two problems?
9:48 am on Mar 5, 2012 (gmt 0)

New User

5+ Year Member

joined:Mar 2, 2012
votes: 0

I had some sucess with this:

RewriteRule ^news\.xml$ /index.php?feed=rss2 [L]

That makes mysite.co.uk/rss2.xml work but if I put that into mysite.co.uk/news/rss2.xml (as below) then it returns one of WP's "this is not a valid feed template" errors.

RewriteRule ^news/rss2.xml$ index.php?feed=rss2

How can I get around this?
6:43 pm on Mar 10, 2012 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 1, 2006
posts: 66
votes: 0

Are we talking here about a server making an http connection to itself?
7:25 pm on Mar 10, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 12, 2006
votes: 92

you could try recreating the feed yourself, which is pretty easy. all you've got to do is put a new function inside the functions.php file (in the same directory as your theme)

then you could output the results as a simple .txt file, or you could save the output to a buffer and then store it as a file on your server -- bypassing CURL completely. you've just got to include whatever you want inside the function and wordpress will take care of the rest.

here is a little example of how you can create a feed.

function NAME_OF_YOUR_FEED() {

header('Content-Type: text/xml; charset='.get_option('blog_charset'), true);

echo '<?xml version="1.0" encoding="UTF-8"?>';

echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';

echo '<channel>';



<atom:link href="http://www.example.com/?feed=NAME_OF_YOUR_FEED" rel="self" type="application/rss+xml" />

// build the feed here with a loop, using query_posts(), or whatever

echo '</channel>';

echo '</rss>'; }


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members