Chris Jinks Description: Basic XML sitemap generator for submission to Google News Installation: ============================================================================== 1. Upload `google-news-sitemap-generator` directory to the `/wp-content/plugins/` directory 2. Activate the plugin through the 'Plugins' menu in WordPress 3. Move the file "google-news-sitemap.xml" into your blog root directory and CHMOD to 777 so it is writable 4. Save/publish/delete a post to generate the sitemap Release History: ============================================================================== 2008-08-04 v1.00 First release 2008-08-17 v1.1 Compatible with new Wordpress database taxonomy (>2.3) 2008-10-11 v1.2 Improved installation instructions, admin panel, general bug fixing 2009-07-27 v1.3 Exclude category options, scheduled posts now supported, UI improved. 2009-08-30 v1.3.1 Addition of XML version/encoding tag to beginning of sitemap 2009-11-11 v1.4 Update to new Google News Sitemap format 2010-03-13 v1.5 Update to new Google News Sitemap format */ /* Copyright 2008 Chris Jinks / David Stansbury Original concept: David Stansbury - http://www.kb3kai.com/david_stansbury/ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ function get_category_keywords($newsID) { global $wpdb; //Check for new >2.3 Wordpress taxonomy if (function_exists("get_taxonomy") && function_exists("get_terms")) { //Get categoy names $categories = $wpdb->get_results(" SELECT $wpdb->terms.name FROM $wpdb->term_relationships, $wpdb->term_taxonomy, $wpdb->terms WHERE $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id AND $wpdb->term_relationships.object_id = $newsID AND $wpdb->term_taxonomy.taxonomy = 'category'"); $i = 0; $categoryKeywords = ""; foreach ($categories as $category) { if ($i>0){$categoryKeywords.= ", ";} //Comma seperator $categoryKeywords.= $category->name; //ammed string $i++; } //Get tags $tags = $wpdb->get_results(" SELECT $wpdb->terms.name FROM $wpdb->term_relationships, $wpdb->term_taxonomy, $wpdb->terms WHERE $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id AND $wpdb->term_relationships.object_id = $newsID AND $wpdb->term_taxonomy.taxonomy = 'post_tag'"); $i = 0; $tagKeywords = ""; foreach ($tags as $tag) { if ($i>0){$tagKeywords.= ", ";} //Comma seperator $tagKeywords.= $tag->name; //ammed string $i++; } } //Old Wordpress database <2.3 else { $categories = $wpdb->get_results("SELECT category_id FROM $wpdb->post2cat WHERE post_id=$newsID"); $i = 0; $categoryKeywords = ""; foreach ($categories as $category) { if ($i>0){$categoryKeywords.= ", ";} //Comma seperator $categoryKeywords.= get_catname($category->category_id); //ammed string $i++; } } if (get_option('googlenewssitemap_tagkeywords') == 'on') { if($tagKeywords!=NULL) { $categoryKeywords = $categoryKeywords.', '.$tagKeywords; //IF tags are included } } return $categoryKeywords; //Return post category names as keywords } function write_google_news_sitemap() { global $wpdb; // Fetch options from database $permalink_structure = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name='permalink_structure'"); $siteurl = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name='siteurl'"); // Output XML header // Begin urlset $xmlOutput.= "\n"; //Credit $xmlOutput.= "\n"; $xmlOutput.= "\n"; $xmlOutput.= "\n"; //Show either Posts or Pages or Both if (get_option('googlenewssitemap_includePages') == 'on' && get_option('googlenewssitemap_includePosts') == 'on') $includeMe = 'AND (post_type="page" OR post_type = "post")'; elseif (get_option('googlenewssitemap_includePages') == 'on') $includeMe = 'AND post_type="page"'; elseif (get_option('googlenewssitemap_includePosts') == 'on') $includeMe = 'AND post_type="post"'; //Exclude categories if (get_option('googlenewssitemap_excludeCat')<>NULL) { $exPosts = get_objects_in_term(get_option('googlenewssitemap_excludeCat'),"category"); $includeMe.= ' AND ID NOT IN ('.implode(",",$exPosts).')'; } //Limit to last 2 days, 50,000 items $rows = $wpdb->get_results("SELECT ID, post_date_gmt, post_title FROM $wpdb->posts WHERE post_status='publish' AND (DATEDIFF(CURDATE(), post_date_gmt)<=2) $includeMe ORDER BY post_date_gmt DESC LIMIT 0, 50000"); // Output sitemap data foreach($rows as $row){ $xmlOutput.= "\t\n"; $xmlOutput.= "\t\t"; $xmlOutput.= get_permalink($row->ID); $xmlOutput.= "\n"; $xmlOutput.= "\t\t\n"; $xmlOutput.= "\t\t\t\n"; $xmlOutput.= "\t\t\t\t"; $xmlOutput.= htmlspecialchars(get_option('blogname')); $xmlOutput.= "\n"; $xmlOutput.= "\t\t\t\t"; $xmlOutput.= get_option('rss_language'); $xmlOutput.= "\n"; $xmlOutput.= "\t\t\t\n"; $xmlOutput.= "\t\t\t"; $thedate = substr($row->post_date_gmt, 0, 10); $xmlOutput.= $thedate; $xmlOutput.= "\n"; $xmlOutput.= "\t\t\t"; $xmlOutput.= htmlspecialchars($row->post_title); $xmlOutput.= "\n"; $xmlOutput.= "\t\t\t"; //Use the categories for keywords $xmlOutput.= get_category_keywords($row->ID); $xmlOutput.= "\n"; $xmlOutput.= "\t\t\n"; $xmlOutput.= "\t\n"; } // End urlset $xmlOutput.= "\n"; $xmlOutput.= ""; $xmlFile = "../google-news-sitemap.xml"; $fp = fopen($xmlFile, "w+"); // open the cache file "google-news-sitemap.xml" for writing fwrite($fp, $xmlOutput); // save the contents of output buffer to the file fclose($fp); // close the file } if(function_exists('add_action')) //Stop error when directly accessing the PHP file { add_action('publish_post', 'write_google_news_sitemap'); add_action('save_post', 'write_google_news_sitemap'); add_action('delete_post', 'write_google_news_sitemap'); add_action('transition_post_status', 'write_google_news_sitemap',10, 3); //Future scheduled post action fix //Any changes to the settings are executed on change add_action('update_option_googlenewssitemap_includePosts', 'write_google_news_sitemap', 10, 2); add_action('update_option_googlenewssitemap_includePages', 'write_google_news_sitemap', 10, 2); add_action('update_option_googlenewssitemap_tagkeywords', 'write_google_news_sitemap', 10, 2); add_action('update_option_googlenewssitemap_excludeCat', 'write_google_news_sitemap', 10, 2); } else //Friendly error message :) { ?>

Accessing this file directly will not generate the sitemap.

The sitemap will be generated automatically when you save/pubish/delete a post from the standard Wordpress interface.

Instructions

1. Upload `google-news-sitemap-generator` directory to the `/wp-content/plugins/` directory
2. Activate the plugin through the 'Plugins' menu in WordPress
3. Move the file "google-news-sitemap.xml" into your blog root directory and CHMOD to 777 so it is writable
4. Save/publish/delete a post to generate the sitemap