Is it possible to continually update an xml file on the server with data from a wordpress page? If so, how could I do it?

I use this plugin http://plugins.elliotcondon.com/advanced-custom-fields/ to create the custom data. Would I be able to save this to an xml file?

It's important that this xml file is updated every time a new page is created or edited.

3 Answers 3


The save_post hook gets called each time a post is created or updated. Just hook into that. There you can do something like fputcsv('/tmp/' . $post_id, $post) or output to XML/JSON.

== EDIT ==

add_action( 'save_post', 'wp239s5_post_to_xml', 10, 2 );
function wp239s5_post_to_xml( $post_id, $post ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
                    return $post_id;

    //convert $post to xml here 
  • Thanks. Have you a bit more comprehensive code example? I will have around 120 pages within a parent that I want to save their custom data to a single xml file, would that still be possible? Any new pages need to be added to it aswell as updated if any details change.
    – Rob
    Commented Nov 1, 2011 at 14:51
  • Well converting the post data to an XML and saving it to a file should be probably be addressed over at StackOverflow. That is a whole discussion on its own. The save_post hook is called each time a page/post is updated/published. I added a small snippet above which shows how to use the save_post hook. As for converting all your older posts/pages, I'm not really sure the best approach. I probably would just do a WP_Query once to query all your old posts and dump them into the XML file.
    – v0idless
    Commented Nov 1, 2011 at 15:15

Yes it is possible, I would recommend looking at PHP's SimpleXML functions or XMLWriter, and then hook it into save_post.


I recently made a site map that would save my post to the xml file, change the fields around for what you want to put in them but this most likely will solve your problem.

the functions you want to look into is

 $fp = fopen(ABSPATH . "sitemap.xml", 'w');
      fwrite($fp, $sitemap);

heres where the fun begins

add_action("publish_post", "eg_create_sitemap");
add_action("publish_page", "eg_create_sitemap");

function eg_create_sitemap() {
  $postsForSitemap = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'modified',
    'post_type'  => array('post'),
    'order'    => 'ASC',
    'post_status'  => 'publish'

  $sitemap = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
  $sitemap .= '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">'."\n";
  /*$sitemap .= '<?xml-stylesheet type="text/xsl" href="http://sitehere.com/sitemap.xsl"?>'."\n";*/

  foreach($postsForSitemap as $post) 

            $post_id = $post->ID;
            $postdat = $post->post_date;
            $postdate = explode(" ", $postdat);
            $postdatee = $postdate[0]."T".$postdate[1]."-08:00"; 
            $postContent = $post->post_excerpt;
            $postName = $post->post_name;
            $posttitle = $post->post_title;
            $postimg = get_post_meta($post_id, 'image', true);
            $postviews = get_post_meta($post_id, 'views', true);
            $postrating = get_post_meta($post_id, 'ratings_average', true);
            $postCategorys = get_the_category($post_id);
            $posttags = get_the_tags($post_id);
            $post_contentloc = get_post_meta($post_id, 'enclosure', true);
            $post_content_loc =  explode("\n", $post_contentloc);
            $abspath = get_site_url();
            $postcat = $postCategorys[0]->category_nicename;

            $sitemap .= '<url>'."\n".
                  "\t\t".'<video:title>'. $posttitle .'</video:title>'."\n".
                  "\t\t".'<video:publication_date>'. $postdatee .'</video:publication_date>'."\n".
                  "\t\t".'<video:description>'. $postContent .'</video:description>'."\n".
                  "\t\t".'<video:view_count>'. $postviews .'</video:view_count>'."\n".
                  "\t\t".'<video:rating>'. $postrating .'</video:rating>'."\n".
                  "\t\t".'<video:player_loc allow_embed="yes" autoplay="ap=1">http://sitehere.com/wp-content/uploads/jw-player-plugin-for-wordpress/player/player.swf</video:player_loc>'."\n".
            if ($postCategorys) {
                  foreach($postCategorys as $postCategory) {
                    $sitemap .= "\t\t".'<video:category>'.$postCategory->cat_name.'</video:category>'."\n".
                        "\t\t".'<video:gallery_loc title="'.$postCategory->cat_name.'">'.$abspath."/".$postCategory->category_nicename.'</video:gallery_loc>'."\n";


            if ($posttags) {
                  foreach($posttags as $tag) {
                    $sitemap .= "\t\t".'<video:tag>'.$tag->name . ' '.'</video:tag>'."\n"; 

            $sitemap .="\t\t".'<video:uploader info="http://sitehere.com">blah blah</video:uploader>'."\n".

  $sitemap .= '</urlset>';

  $fp = fopen(ABSPATH . "sitemap.xml", 'w');
  fwrite($fp, $sitemap);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.