Events Made Easy Forums Tips TIP: Generate a dynamic sitemap of events

Tagged: 

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #43797
    Anonymous
    Inactive

    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&#8217;); ?>

    <?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″>&#8221; . $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…

    #49708
    Franky
    Keymaster

    Hmm … I don’t recall ever saying anything specific about sitemaps, but anyway: thanks for the tip!

    #49710
    Anonymous
    Inactive

    OK – 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";

    ?>

    #49711
    Franky
    Keymaster

    I 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)

    #49712
    Franky
    Keymaster

    I 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.

    #49713
    Anonymous
    Inactive

    Hi 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

    #49714
    Franky
    Keymaster

    Yes, I know why you created the sitemap, but why not submit an RSS feed?

    #49715
    Anonymous
    Inactive

    Hello,

    The query results “Sitemap Updated” but the sitemap file is empty. Anyone can help me, please?

    #49716
    Franky
    Keymaster

    Please create a new thread for your problem and reference this thread.

    #49717
    Anonymous
    Inactive

    Hi 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";
    ?>

    #49718
    Anonymous
    Inactive

    This 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 🙂

    #49719
    Franky
    Keymaster

    Thanks for the contrib Tom, but why not use eme_get_events() for the events query?

    #49720
    Anonymous
    Inactive

    Because 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.

Viewing 13 posts - 1 through 13 (of 13 total)
  • The forum ‘Tips’ is closed to new topics and replies.
Scroll to Top