Save page data to an xml file

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?

Read More

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

Related posts

Leave a Reply

3 comments

  1. 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 
    }
    
  2. 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".
                      "tt".'<video:thumbnail_loc>'.$postimg.$post_custom[image].'</video:thumbnail_loc>'."n".
                      "tt".'<video:title>'. $posttitle .'</video:title>'."n".
                      "tt".'<video:publication_date>'. $postdatee .'</video:publication_date>'."n".
                      "tt".'<video:description>'. $postContent .'</video:description>'."n".
                      "tt".'<video:content_loc>'.$post_content_loc[0].'</video:content_loc>'."n".
                      "tt".'<video:view_count>'. $postviews .'</video:view_count>'."n".
                      "tt".'<video:rating>'. $postrating .'</video:rating>'."n".
                      "tt".'<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".
                      "tt".'<video:requires_subscription>no</video:requires_subscription>'."n";
                if ($postCategorys) {
                      foreach($postCategorys as $postCategory) {
                        $sitemap .= "tt".'<video:category>'.$postCategory->cat_name.'</video:category>'."n".
                            "tt".'<video:gallery_loc title="'.$postCategory->cat_name.'">'.$abspath."/".$postCategory->category_nicename.'</video:gallery_loc>'."n";
                      }
    
                    }
    
                if ($posttags) {
                      foreach($posttags as $tag) {
                        $sitemap .= "tt".'<video:tag>'.$tag->name . ' '.'</video:tag>'."n"; 
                      }
                    }
    
                $sitemap .="tt".'<video:uploader info="http://sitehere.com">blah blah</video:uploader>'."n".
                        "tt".'<video:family_friendly>yes</video:family_friendly>'."n".
                    "t".'</video:video>'."n".  
                '</url>'."nn";
      }
    
      $sitemap .= '</urlset>';
    
      $fp = fopen(ABSPATH . "sitemap.xml", 'w');
      fwrite($fp, $sitemap);
      fclose($fp);
    }
    
    ?>