Anyone care to collaborate or improve this Pods XML-sitemap generator
Hi,
while busy with optimizing the site, more specifically XML-sitemaps. Since Google XML Generator only has an option for 'some extra values', but and I don't feel like inputting x 1000 urls.., I decided to try to build my own xml-sitemap generator. For now, I just set it up as a template, just assign this template to a page (e.g. XML-sitemap), point your robots.txt to this page
but first, add your pods / pods-slugs to the array..
anyone who wants to improve the code, feel free..
<?php
/* Template Name: Sitemap_XML */ $siteurl = get_option('home'); $podsarray = array( // list of your arrays // list your pods as a key-value pair (podname => Detail Page slug) // e.g. podname can be 'event', while the event-detailpages reside at blogname/events/, key-value will be 'event' => 'events' 'skidorp' => 'skidorpen', 'skigebied' => 'skigebieden', 'place' => 'place', 'recommendedplace' => 'recommendedplace', 'skipiste' => 'skipistes', 'skitest_gear' => 'skitest', 'store' => 'skimerken' );
// Start XML output
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("content-type:text/xml;charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
echo '<urlset>';
// Loop through pods with key-value
foreach ($podsarray as $k => $v) {
$freq = 'monthly';
$priority = '0.6';
$Record = new Pod($k);
$Record->findRecords('name ASC', 5000);
if (0 < $Record->getTotalRows()) {
while ($Record->fetchRecord()) {
$slug = $siteurl . '/' . $v . '/' . htmlspecialchars($Record->get_field('slug'));
$mod = htmlspecialchars($Record->get_field('modified'));
$freq = 'monthly';
echo '<url>';
echo '<loc>' . $slug . '</loc>';
echo '<lastmod>' . $mod . '</lastmod>';
echo '<changefreq>' . $freq . '</changefreq>';
echo '<priority>' . $priority . '</priority>';
echo '</url>';
}
}
}
echo '</urlset>';
?>
the frequency stuff and priority is something I haven't got to..
I guess maybe it's interesting to let pod x have a frequency rate of weekly, priority 0.8 while others rather be monthly and priority 0.5 ..
7 Answers
I've been playing with this since I saw this original post.
The key is you have to create this php file as a plugin. Here is a first crack at it. To use it, create a folder called pods-sitemap-addin in your /wp-content/plugins/ folder. The code contents below go in a file in there called pods-sitemap-addin.php. Once you've done that, go to your plugins page and enable it.
You'll need to edit the area starting at lines 19-21 to match your setup. I'm going to flesh this out over the next few weeks and try to make this into a real WP-plugin with an fancy admin page and all that.
I welcome any comments.
<?php
/*
Plugin Name: Pods Google XML Sitemap Adder
Plugin URI: http://podscms.org/qna/questions/1422/anyone-care-to-collaborate-or-improve-this-pods-xml-sitemap-generator
Description: Extends the Google XML Sitemaps plugin to add pages generated in the Pods Plugin to the generated sitemap. This is an Alpha version. This plugin file must be edited for functionality.
Version: 0.1a
Author: Chris Pilko
Author URI: http://pilkotech.com/
License: GPL2
*/
function pod_pages() {
$generatorObject = &GoogleSitemapGenerator::GetInstance(); //Please note the "&" sign!
if($generatorObject!=null):
$siteurl = get_option('home');
$pod_to_map = array(); //Array to hold pods to add to the sitemap.
// Add your pods:
// Copy the following line once for each pods detail page you have.
$pod_to_map[] = array('pod_name' => 'brands', 'pod_page' => 'brands', 'slug_col'=>'permalink', 'update_freq' => 'weekly', 'priority' => 0.5, 'where_query' => 't.publish = true');
$pod_to_map[] = array('pod_name' => 'products', 'pod_page' => 'products', 'slug_col'=>'permalink', 'update_freq' => 'weekly', 'priority' => 0.5, 'where_query' => 't.publish = true');
$pod_to_map[] = array('pod_name' => 'product_category', 'pod_page' => 'product-list', 'slug_col'=>'permalink', 'update_freq' => 'weekly', 'priority' => 0.5, 'where_query' => 't.publish = true');
foreach ($pod_to_map as $pod_item) {
$Record = new Pod($pod_item['pod_name']);
$Record -> findRecords('id ASC', -1, $pod_item['where_query']);
if (0 < $Record->getTotalRows()) {
while ($Record->fetchRecord()) {
$url = $siteurl . '/' . $pod_item['pod_page'] . '/' . $Record->get_field($pod_item['slug_col']);
$time = $Record->get_field('modified');
$generatorObject->AddUrl($url,$time,$pod_item['update_freq'],$pod_item['priority']);
//echo "<tr><td>" . $url . '</td><td>' . $time . '</td><td>' . $pod_item['update_freq'] . '</td><td>' . $pod_item['priority'] . "</td></tr>\n";
}
}
}
endif;
}
add_action("sm_buildmap","pod_pages");
/*
//Uncomment this and line 29 for debugging,
<table>
<tr><td>URL</td><td>Time</td><td>Update Freq</td><td>Priority</td></tr>
<?php pod_pages(); ?>
</table>
*/
?>
Great idea! Scott is actually working on his Search Engine WordPress plugin that will generate an XML sitemap after indexing has been completed, perhaps that could be extrapolated and turned into a standalone plugin as it'd be very valuable to lots of developers.
This also works great for me.
Only a small modification including the example of usage, and also ORDER BY modified DESC (the last change always on top):
<?php
/*
Template Name: Sitemap XML for Pods Data
*/
$siteurl = get_option('home');
$podsarray = array('postoffices'=>'postoffices' ); // <-- The sample of usage
// list of your arrays
// list your pods as a key-value pair (podname => detail-page-slug)
// e.g. podname can be 'event', while the event-detailpages reside at blogname/events/, key-value will be 'event' => 'events' 'skidorp' => 'skidorpen', 'skigebied' => 'skigebieden', 'place' => 'place', 'recommendedplace' => 'recommendedplace', 'skipiste' => 'skipistes', 'skitest_gear' => 'skitest', 'store' => 'skimerken' );
// Start XML output
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("content-type:text/xml;charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><?xml-stylesheet type=\"text/xsl\" href=\"http://www.posindonesia.org/wp-content/plugins/google-sitemap-generator/sitemap.xsl\"?><!-- generator=\"wordpress/3.0.1\" -->"; // <-- if you are also running XML Google Sitemap plugin, adjust with your XML Sitemap's style location, so the output of XML Sitemap will be same with XML Google Sitemap style.
echo '<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">';
// Loop through pods with key-value
foreach ($podsarray as $k => $v) {
$freq = 'monthly';
$priority = '0.6';
$Record = new Pod($k);
$Record->findRecords('modified DESC', 5000);
if (0 < $Record->getTotalRows()) {
while ($Record->fetchRecord()) {
$slug = $siteurl . '/' . $v . '/' . htmlspecialchars($Record->get_field('slug'));
$mod = htmlspecialchars($Record->get_field('modified'));
$freq = 'monthly';
echo '<url>';
echo '<loc>' . $slug . '</loc>';
echo '<lastmod>' . $mod . '</lastmod>';
echo '<changefreq>' . $freq . '</changefreq>';
echo '<priority>' . $priority . '</priority>';
echo '</url>';
}
}
}
echo '</urlset>';
?>
edited Sep 9 '10 at 6:22
hey guys i used this code to generate data for Pod Pages, what i was tryin to achieve is have a combined XML sitemap with the WordPress sitemap also
a way to do this was to extend XML sitemaps build process, which the documentation explains to be done by
function pods_pages() {
//generate pod pages
}
add_action("sm_buildmap","pods_pages");
"sm_buildmap" is a hook to which the above function can be used to append the Pod Links, i jus cant figure out where to do it since running a script manually as a PHP file execures the Pod Queries rite but WordPress Fails to acknowledge the the Function which is attached to XML Sitemaps Hook, & Tried to Run the Code directly in the XML Sitemap File sitemap-core.php too but got SQL errors instead there seems PODS wasn't recognised
Now here is my Code, if some1 could help would be gr8 thanks!
<?php
/* Template Name: Sitemap_XML */
include 'wp-blog-header.php';
function pods_pages() {
$siteurl = get_option('home');
$podsarray = array('brand'=>'brands');
// Loop through pods with key-value
foreach ($podsarray as $k => $v) {
$freq = 'weekly';
$priority = '0.8';
$Record = new Pod($k);
$Record->findRecords('modified DESC', 5000);
if (0 < $Record->getTotalRows()) {
while ($Record->fetchRecord()) {
$slug = $siteurl . '/' . $v . '/' . htmlspecialchars($Record->get_field('brandurl'));
$mod = htmlspecialchars($Record->get_field('modified'));
$freq = 'weekly';
$links['url'][]= $slug;
$links['last_mod'][]= $mod;
$links['change_freq'][]= $freq;
$links['priority'][]= $priority;
}
}
}
$count = count($links['url']);
$generatorObject = &GoogleSitemapGenerator::GetInstance(); //Please note the "&" sign for PHP4!
if($generatorObject!=null){
for($i=0; $i<$count; $i++)
{
$o.= '<url>';
$o.= '<loc>' . $links['url'][$i] . '</loc>';
$o.= '<lastmod>' . $links['last_mod'][$i] . '</lastmod>';
$o.= '<changefreq>' . $links['change_freq'][$i] . '</changefreq>';
$o.= '<priority>' . $links['priority'][$i] . '</priority>';
$o.= '</url>';
//echo $o."<br />";
$generatorObject->AddUrl($links['url'][$i],$links['last_mod'][$i],$links['change_freq'][$i],$links['priority'][$i]);
}
}
}
add_action("sm_buildmap","pods_pages");
?>
this was an external PHP file btw!
glad to see some folks actually found a way of using it..will look into your modified version acenik
This seems to be a bit complicated, but it validates.
template file "template-pods-sitemap.php" inside theme dir:
<?php
/*
Template Name: Sitemap XML for Pods Data
*/
# function to get a valid date & time format
function get_google_valid_sitemap_date($slug) {
$Record = new Pod('store',$slug);
$mod = $Record->get_field('modified');
$m = new DateTime($mod);
# this one does returns a valid google sitemap date
# like: 2010-10-13T15:32:38+00:00 from the "modified"-column
return $m->format(DateTime::ATOM);
}
# fire up the (podlist) content
pods_content();
?>
"pods template" in pods database called "sitemap" with this content
<url>
<loc><?php echo get_option('home'); ?>/<?php echo pods_url_variable(0); ?>/{@slug}</loc>
<lastmod><?php $slug = $this->get_field('slug'); ?><?php echo get_google_valid_sitemap_date($slug); ?></lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
"pods page" with the name/address "your_pod_name/sitemap.xml"
<?php
$podsarray = array( 'your_pod_name' => 'slug' );
// Start XML output
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("content-type:text/xml;charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><?xml-stylesheet type=\"text/xsl\" href=\"/wp/wp-content/plugins/google-sitemap-generator/custom.xsl\"?>";
echo '<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">';
// Loop through pods with key-value
foreach ($podsarray as $k => $v) {
$Record = new Pod($k);
$Record->findRecords('modified DESC', 5000);
if (0 < $Record->getTotalRows()) {
while ($Record->fetchRecord()) {
# fire up the template above
echo $Record->showTemplate('sitemap');
}
}
}
echo '</urlset>';
?>
Produces a valid codefor office sitemap.xml - look here
Hope I could help someone of you guys. Bye.
I am having issues with these examples of allowing search engines to crawl pods data and looking for any suggestions if these are still the only way that pods data can be crawled by search engines. Any code examples or current updates to this page would be appreciated.


