Events Made Easy › Forums › Tips › TIP: Generate a dynamic sitemap of events
Tagged: sitemap
- This topic has 12 replies, 3 voices, and was last updated 11 years ago by Anonymous.
-
AuthorPosts
-
Sun 29 Apr 2012 at 13:11 #43797AnonymousInactive
I know there are a lot of questions over whether sitemaps are required or not which is why I accept Franky’s decision to prevent this from being a feature, however I wanted to ensure that my events were all submitted to Google via a dedicated events sitemap and thought I’d share my solution hoping that it may help someone.
I know it’s messy and any suggestions are welcome:
<?php require(‘wp-blog-header.php’); ?>
<?php include(‘http://www.yourdomain.com/wp-content/plugins/events-made-easy/eme_functions.php’); ?>
<?php
ob_start(); // Turn on output buffering
$eme_sitemap_data = eme_get_events_list(‘limit=0&scope=all&order=DESC&format=<url>’ . “n” . ‘ <loc>#_EVENTPAGEURL</loc>’ . “n” . ‘ <priority>0.7</priority>’ . “n” . ‘</url>’);
$sitemap = ob_get_contents();
ob_end_clean();
// Build XML Sitemap
$sitemap_data = “<?xml version=”1.0″ encoding=”UTF-8″?>n” . “<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″>” . $sitemap . “n” . “</urlset>”; ?>
<?php
// Write to XML file
$myFile = “events-sitemap.xml”;
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);
fwrite($fh, $sitemap_data);
fclose($fh);
echo “Sitemap generated”;
?>
Then you can set a scheduled task to run the script every hour or so…
Mon 30 Apr 2012 at 22:54 #49708FrankyKeymasterHmm … I don’t recall ever saying anything specific about sitemaps, but anyway: thanks for the tip!
Tue 1 May 2012 at 11:45 #49710AnonymousInactiveOK – I completely re-wrote this as I wanted to show the modified date in the <lastmod> tag as there was no EME placeholder to use. (I’m using the value from modif_date in the eme_events table.)
Here’s my next version:
<?php
require('/path/to/httpdocs/wp-blog-header.php');
global $wpdb;
$sql = "SELECT * FROM " . $wpdb->prefix . "eme_events ORDER BY 'wp_eme_events'.'event_start_date' DESC";
$events = $wpdb->get_results($sql); // Run our query, getting results as an object
if (!empty($events)) { // If the query returned something
$header = "<?xml version="1.0" encoding="UTF-8"?>n" . "<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">n";
$urls = "";
$footer = "</urlset>";
foreach ($events as $event) { // Loop though our results!
// Build Sitemap Elements
$locurl = "http://www.yourdomain.com/events/$event->event_id/$event->event_slug";
// Format the date
$lastmod = date('Y-m-d', strtotime($event->modif_date));
// Concatenate List of URLs
//$urls .= ' <url>' . "n" . ' <loc>' . $locurl . '</loc>' . "n" . ' <priority>0.5</priority>' . "n" . ' <lastmod>' . $lastmod . '</lastmod>' . "n" . ' </url>' . "n";
$urls .= " <url>n <loc>$locurl</loc>n <priority>0.5</priority>n <lastmod>$lastmod</lastmod>n </url>n";
}
// Print Sitemap
$xmlsitemap = $header . $urls . $footer;
//echo $xmlsitemap;
}
// Write to file
$myFile = "/path/to/httpdocs/sitemap.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $xmlsitemap);
fclose($fh);
echo "Sitemap Updated";
?>Tue 1 May 2012 at 21:24 #49711FrankyKeymasterI edited it because of backtick problems, you might want to review the resulting code (no telling what got lost because of bbpress issues with backticks)
Wed 24 Apr 2013 at 11:05 #49712FrankyKeymasterI just took another look at this, and was wondering: if you submit this to google, why not just submit the RSS feed with “scope=all” to google (which seems an accepted format as well)?
I can add the code of course, but need to understand this 🙂 Also: your query doesn’t take into account private events.
Thu 25 Apr 2013 at 07:17 #49713AnonymousInactiveHi Franky,
The reason for this was that my events weren’t getting indexed by Google.
After submitting a Sitemap to Google Webmaster Tools then the events started getting indexed.
I can’t ‘guarantee’ that was the reason, but it certainly made it easier to see that events were getting added. Also it allowed me to tell google how often to re-index and when the last modified date was.
Thanks,
Tom
Thu 25 Apr 2013 at 08:02 #49714FrankyKeymasterYes, I know why you created the sitemap, but why not submit an RSS feed?
Thu 5 Dec 2013 at 19:17 #49715AnonymousInactiveHello,
The query results “Sitemap Updated” but the sitemap file is empty. Anyone can help me, please?
Thu 5 Dec 2013 at 22:54 #49716FrankyKeymasterPlease create a new thread for your problem and reference this thread.
Sat 7 Dec 2013 at 12:27 #49717AnonymousInactiveHi guys,
Just came across this.
The reason for my code and not the RSS version was the value lastmod wasn’t possible via RSS.
I changed my code again so see below:
<?php
error_reporting('E_ALL'); // Suppress errors on production server
require('/path/to/httpdocs/wp-blog-header.php');
global $wpdb;
//$sql = "SELECT * FROM " . $wpdb->prefix . "eme_events ORDER BY <code>wp_eme_events</code>.<code>event_start_date</code> DESC";
$sql = "SELECT * FROM " . $wpdb->prefix . "eme_events ORDER BY <code>wp_eme_events</code>.<code>event_id</code> DESC LIMIT 5000"; // Show 5000 most recently added to prevent server timeout
$events = $wpdb->get_results($sql); // Run our query, getting results as an object
if (!empty($events)) { // If the query returned something
$header = "<?xml version="1.0" encoding="UTF-8"?>n" . "<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">n";
$urls = "";
$footer = "</urlset>";
foreach ($events as $event) { // Loop though our results!
// Build Sitemap Elements
$locurl = "http://plainandsimple.tv/event/$event->event_id/$event->event_slug";
// Format the date - also in case some EME Events have 0000-00-00 date format, manually add 1st Jan 2012
if(strtotime($event->modif_date) > strtotime('2010-01-01 00:00')){
$lastmod = date('Y-m-d', strtotime($event->modif_date));
} else {
$lastmod = date('Y-m-d', strtotime('2010-01-01 00:00'));
}
// Make future events higher priority
if(strtotime($event->event_start_date) > strtotime('today')){
$priority = 0.9;
$changefreq = daily;
} else {
$priority = 0.3;
$changefreq = monthly;
}
// Concatenate List of URLs
$urls .= " <url>n <loc>$locurl</loc>n <lastmod>$lastmod</lastmod>n <changefreq>$changefreq</changefreq> <priority>$priority</priority>n </url>n";
}
// Print Sitemap
$xmlsitemap = $header . $urls . $footer;
}
// Encode as UTF8 then Write to file
$myFile = "/path/to/httpdocs/club-listings-sitemap.xml";
$fh=fopen($myFile,"w");
fwrite($fh,utf8_encode($xmlsitemap));
fclose($fh);
echo "Sitemap Updated";
?>Sat 7 Dec 2013 at 12:29 #49718AnonymousInactiveThis next one generates a sitemap for venues. I changed the query as the lastmod value uses the lastmod value for the most recent event at that venue as this was what I consider to be the latest version of that ‘Page’ as far as google should be concerned:
<?php
error_reporting(E_ALL);
require('/path/to/httpdocs/wp-blog-header.php');
global $wpdb;
//$sql = "SELECT * FROM " . $wpdb->prefix . "eme_locations ORDER BY <code>location_id</code> DESC";
// Note this query will only pull in Locations that have events
$sql = "SELECT loc.location_name, loc.location_id, MAX(ev.modif_date) lastmodif FROM wp_eme_events ev INNER JOIN wp_eme_locations loc ON ev.location_id = loc.location_id GROUP BY ev.location_id";
$events = $wpdb->get_results($sql); // Run our query, getting results as an object
if (!empty($events)) { // If the query returned something
$header = "<?xml version="1.0" encoding="UTF-8"?>n" . "<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">n";
$urls = "";
$footer = "</urlset>";
foreach ($events as $event) { // Loop though our results!
// Build Sitemap Elements
$name = strtolower($event->location_name);
//Strip any unwanted characters
$name = preg_replace("/[^a-z0-9_s-]/", "", $name);
//Clean multiple dashes or whitespaces
$name = preg_replace("/[s-]+/", " ", $name);
//Convert whitespaces and underscore to dash
$name = preg_replace("/[s_]/", "-", $name);
$locurl = "http://plainandsimple.tv/event-listings/" . $event->location_id . "/" . $name;
if(strtotime($event->lastmodif) > strtotime('2010-01-01 00:00')){
$lastmod = date('Y-m-d', strtotime($event->lastmodif));
} else {
// Format the date in case some EME Events have 0000-00-00 date format, manually add 1st Jan 2012
$lastmod = date('Y-m-d', strtotime('2012-01-01 00:00'));
}
// Concatenate List of URLs
$urls .= " <url>n <loc>$locurl</loc>n <lastmod>$lastmod</lastmod>n <changefreq>weekly</changefreq> <priority>0.8</priority>n </url>n";
}
// Print Sitemap
$xmlsitemap = $header . $urls . $footer;
}
// Write to file
$myFile = "/path/to/httpdocs/club-locations-sitemap.xml";
$fh=fopen($myFile,"w");
fwrite($fh,utf8_encode($xmlsitemap));
fclose($fh);
echo "Sitemap Updated";
?>Hope someone finds it useful 🙂
Sat 7 Dec 2013 at 16:27 #49719FrankyKeymasterThanks for the contrib Tom, but why not use eme_get_events() for the events query?
Sun 8 Dec 2013 at 03:50 #49720AnonymousInactiveBecause I did it before I’d really tried the eme functions outside of the shortcodes.
Maybe I’ll re-work it and post again.
I should also be using site_url() as well instead of hard-coded URLs.
-
AuthorPosts
- The forum ‘Tips’ is closed to new topics and replies.