Make wordpress generate a custom XML file(not a sitemap) that is updated only when new content is posted?

I’m not looking for a sitemap, I’m looking to create an xml file that I can have a flash menu access, but I don’t want to generate the XML file each time someone visits a page, I want to have a static XML to refer to for speed. And I’d like to have wordpress update the file whenever new content is posted(not comments).

Is there anything built into wordpress that would allow me to do this? Any plugins? Again, I don’t want a sitemap. I want a specific list of items. Right now I’ve been using page templates with custom loops, but it takes to long to generate every time.

Related posts

Leave a Reply

3 comments

  1. Hmm… not sure if there are any WordPress-specific functions for creating files. There’s wp_handle_upload, but I’m not sure how you’d use it in this context.

    Depending on your server configuration, you can probably just use fopen and fwrite to do this.

    Keep the code you’re currently using (with your custom loops…I’m assuming they contain the XML markup you want), but have it build the output as a string. Then save that string into a file. Something like:

    // Build your file contents as a string
    $file_contents = '<?xml version="1.0" ?><yourroot><item>some content</item></yourroot></xml>';
    
    // Open or create a file (this does it in the same dir as the script)
    $my_file = fopen("myfile.xml", "w");
    
    // Write the string's contents into that file
    fwrite($my_file, $file_contents);
    
    // Close 'er up
    fclose($my_file);
    

    Of course, this all depends on your server config and permissions. Maybe someone has a better solution that only requires native WP functions.

  2. Nothing built-in but you should look into the various sitemaps plugins. These will show you a) where to hook into and b) how to generate the file if you’re not familiar with php file manipulation.

  3. Take a look at the export_wp function.

    Make a duplicate of it and keep only the stuff you want exported, then run this function every time a post is saved: add_action('save_post', 'my_export_wp');

    (Make so the function sends its output to a xml file)