WordPress Sitemap Generator Plugin

This plugin will create a sitemap for your WordPress site (http://example.com/sitemap.xml). This was originally created for a multi-site setup, but it works fine on a single site install as well. There is no ‘User Interface’ to speak of with this plugin, just drop in place.

You may also download this code via it’s github repository:

First, create a file named sitemap-generator.php in your mu-plugins directory, (or your plugins directory if your using a single install WordPress setup) and past the below into it.

<?php
/*
Plugin Name: Sitemap Generator
Plugin URI: http://technology.mattrude.com/2011/10/07/wordpress-sitemap-generator-plugin/
Description: Automatic generate standard XML sitemap (http://example.com/sitemap.xml) that supports the protocol including Google, Yahoo, MSN, Ask.com, and others. No files stored on your disk, the sitemap.xml file is generate as needed, like your feeds.
Version: 1.0
Author: Matt Rude
Author URI: http://mattrude.com/
*/

function sitemap_flush_rules() {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
}

add_action('init', 'sitemap_flush_rules');

function xml_feed_rewrite($wp_rewrite) {
        $feed_rules = array(
                '.*sitemap.xml$' => 'index.php?feed=sitemap'
        );

        $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}

add_filter('generate_rewrite_rules', 'xml_feed_rewrite');

function do_feed_sitemap() {
        $template_dir = dirname(__FILE__) . '/templates';
        load_template( $template_dir . '/feed-sitemap.php' );
}

add_action('do_feed_sitemap', 'do_feed_sitemap', 10, 1);

?>

Next, create a new directory named templates and past the below in a file named feed-sitemap.php in it.

<?php
/**
 * XML Sitemap Feed Template for displaying XML Sitemap Posts feed.
 */

//header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);

echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

        <!-- Main Site -->

        <url>
                <loc><?php bloginfo_rss('url') ?></loc>
                <lastmod><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></lastmod>
                <changefreq>daily</changefreq>
                <priority>0.8</priority>
        </url>

        <!-- Site Pages -->

<?php
        $args = array(
                'post_type' => 'page',
                'numberposts' => 100,
                'status' => 'publish',
                'orderby' => 'date',
                'order' => 'DESC'
        );
        $post_ids = get_posts($args);

        if ($post_ids) {
                foreach ($post_ids as $post) { ?>
        <url>
                <loc><?php the_permalink_rss() ?></loc>
                <lastmod><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_post_time('Y-m-d H:i:s', true), false); ?></lastmod>
                <changefreq>monthly</changefreq>
                <priority>0.7</priority>
        </url>
<?php
 } }
?>

        <!-- Site Posts -->

<?php
        $args = array(
                'post_type' => 'post',
                'numberposts' => 100,
                'post_status' => 'publish',
                'orderby' => 'date',
                'order' => 'DESC'
        );
        $post_ids = get_posts($args);

        if ($post_ids) {
                foreach ($post_ids as $post) { ?>
        <url>
                <loc><?php the_permalink_rss() ?></loc>
                <lastmod><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_post_time('Y-m-d H:i:s', true), false); ?></lastmod>
                <changefreq>monthly</changefreq>
                <priority>0.5</priority>
<?php
        $args2 = array(
                'post_type' => 'attachment',
                'numberposts' => 200,
                'post_parent' => $post->ID,
                'post_mime_type' => 'image',
                'orderby' => 'date',
                'order' => 'DESC'
        );
        $images = get_posts($args2);
        if ($images) {
                foreach ($images as $post) { ?>
                <image:image>
                        <image:loc><?php echo wp_get_attachment_url(); ?></image:loc>
<?php if ( !empty($post->post_excerpt) ) echo '                 <image:caption>' . esc_html($post->post_excerpt, 1) . '</image:caption>
'; ?>
                        <image:title><?php echo esc_html($post->post_title, 1) ?></image:title>
                </image:image>
<?php } } ?>
        </url>
<?php
                }
        }
?>
</urlset>
Advertisements

Building a WordPress Cloud, Cluster Setup

This how-to will explain how to build a WordPress site on 2, 3, or more Rackspace cloud servers, with full load-balancing and redundancy.

To accomplish this, you will setup multiple web-servers and one or more mySQL servers, behind two Rackspace cloud load-balancers. One load-balancer will server all you normal user internet traffic from all the web-servers.  The other load-balancer will server only your secured traffic to your admin sites (Dashboard) from a single, master, server.  You will then set WordPress to only server the admins sites threw a secure connections, this way all uploads will be saved to a single server and may be distributed from there. This also insures that you will be able to see the newly uploaded file, even before it has a chance to propagate to the other servers. The flaw with this configuration is that if the Master server goes down, no posts may be created until the issue is resolved.

The Setup

There are meny different ways we can accomplish this.  Here I am going to show a two server setup, but you can easily expand this into as many servers as you wish.

  • Server1 – Master Database Server, Master Web Server
  • Server2 – Slave Database Server, Slave Web Server or only Slave Web Server

Building Server1

Server1 is our main server, if Server2+went down, the site would still be fully up, just slower.

This How-To assumes your using Fedora hosts for these setup’s.  To start out, we need Apache, php, mySQL installed on the server.

Moving to the new Rackspace Load balancer

Recently I have moved this site and others to a new load balancer solution by Rackspace (the company who also hosts the servers these sites run on).

Prior to this change, I was running a single 1024mb ram/40gb server, with this change I am now running two, fully master/master mirror 512mb/20gb servers, so the threw put should be a bit faster since there is now two.

I will hold my breath and let you know how it goes.