Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 7 down vote accepted

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 
}
share|improve this answer
    
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 Nov 1 '11 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 Nov 1 '11 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.

share|improve this answer

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);
      fclose($fp);

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) 
  {
            setup_postdata($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".'<loc>'.$abspath.'/'.$postcat."/".$postName.'/</loc>'."\n".
              "\t".'<video:video>'."\n".
                  "\t\t".'<video:thumbnail_loc>'.$postimg.$post_custom[image].'</video:thumbnail_loc>'."\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:content_loc>'.$post_content_loc[0].'</video:content_loc>'."\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".
                  "\t\t".'<video:requires_subscription>no</video:requires_subscription>'."\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".
                    "\t\t".'<video:family_friendly>yes</video:family_friendly>'."\n".
                "\t".'</video:video>'."\n".  
            '</url>'."\n\n";
  }

  $sitemap .= '</urlset>';

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

?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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