Dynamic WordPress Sitemaps with PHP

with No Comments

As they say, it is the little things that mean so much.  Like how to add a human readable dynamic title to a machine generated Google sitemap.  That is just what I am about to show you using a simple little modification anybody with a little edit skills can do.  Best of all you can even do this on your Multisite WordPress install, and ALL domain names will benefit.


BWP-GXS sound like some formula for a new vaccine!  It may be a formula for better WordPress Sitemaps and SEO.  It is the shorthand for a plugin called Better WordPress Google XML Sitemaps brought to us at BetterWP.net/wordpress-plugins/google-xml-sitemaps/

This sitemap generator works great!  The only thing I do not like about it is the Title on the human readable sitemaps.  They are always the same…

XML Sitemap Index
XML Sitemap

That is the actual title.  I kept thinking to myself, wouldn’t it be cool to add the domain name!  Something like…

Alantait.com XML Sitemap Index

However there are some little challenges to doing this.  I could have just made a custom style sheet for these and put in the domain name the way I wanted it.  However…

WordPress MultiSite

I have a WordPress MultiSite install running 50 different domain names!  That would have meant creating 100 custom xsl style sheets, and two additional xsl style sheets for each domain name I added in the future.  I would prefer something easier. However, I decided to take this one step at a time and make custom .xsl files first.

Problem One – .xsl Directories Don’t Work!

Immediately I had a problem. I had made a folder called /sitemap.xsl-files/ and copied the plugin xsl  files:

  • bwp-sitemap.xsl
  • bwp-sitemapindex.xsl

and renamed them:

  • css-sitemap.xsl
  • css-sitemapindex.xsl

I checked to make sure I could see them online at:

  • http://alantait.com/sitemap.xsl-files/css-sitemap.xsl
  • http://alantait.com/sitemap.xsl-files/css-sitemapindex.xsl

Everything was ok so I set the Custom XSLT stylesheet URL in the Sitemap Generator tab of the plugin to:

  • http://alantait.com/sitemap.xsl-files/css-sitemap.xsl

The Failure

What I got was a not very pretty…

http://alantait.com/post.xml 2013-12-08T03:42:53+00:00 http://alantait.com/page.xml 2013-12-08T04:09:06+00:00 http://alantait.com/page_external.xml 2013-12-08T04:09:06+00:00 http://alantait.com/taxonomy_category.xml 2013-12-07T19:42:53+00:00

Solution One

I viewed the page source of that output and saw this line…

<?xml-stylesheet type="text/xsl" href="http://alantait.com/sitemapindex.xsl-files/external-sitemaps/css-sitemapindex.xsl"?>

The plugin had changed my directory…

  • /sitemap.xsl-files/


  • /sitemapindex.xsl-files/

But there was no directory by that name! I realized the problem was the plugin had changed EVERY .xsl to index.xsl, even in the directory name. I didn’t like that, but I changed the directory name to…

  • /external-sitemaps/

Everything worked ok then. Note: I chose /external-sitemaps/ because I have nine non WordPress URL’s in this domain and I plan to add these to an external sitemap later.


PHP can easily add the domain name to any webpage.  So I decide to convert (just rename actually) the .xsl style sheets to .php and add the PHP code.

If you use PHP Short Tags (or if you are not sure that PHP short tags is OFF) change the first line of each of the files…


<?xml version="1.0" encoding="UTF-8"?>


<?php header('Content-type: application/xml'); ?>
<?= '<?' ?>xml version="1.0" encoding="UTF-8"?>

Those two lines make the .php file behave as a .xsl file!

They work perfectly 100% of the time is PHP Short Tags is On or Off!

If you are SURE PHP Short Tags are OFF, you can Use the Above (highly recommended), or you can just add…

<?php header('Content-type: application/xml'); ?>

To the top of each file.

New Names

My files were then called:

  • css-sitemap.php
  • css-sitemapindex.php

They could be accessed via:

  • http://alantait.com/external-sitemaps/css-sitemap.xsl
  • http://alantait.com/external-sitemaps/css-sitemapindex.xsl

It was simple to do and worked fine, except for one hitch…

BWP-GXP Doesn’t Accept PHP Files!

It is almost ironic that a plugin written in PHP will only accept .xls files on the Admin Sitemap Generator page of the plugin!  Here is what I tried…BWP-GXS Sitemap Generator page setting for xsl files


So I decided to look into the BWP-GXP Code and find out why. As it turns out, the code looks for the .xsl at the end of the sitemap file name and replaces it with index.xsl to create the .xsl index file name.

So if you used the example and put in something like…

  • http://example.com/my-stylesheet.xsl

The program would remove the .xsl and add index.xsl so the .xsl file name for the index page would be…

  • http://example.com/my-stylesheetindex.xsl

If you think about it, that is pretty cool!

Problem Two

The problem is, I needed a file name that ended in .php!

I found the code that does this in a file called class-bwp-simple-gxs.php around line 247 (in version 1.2.2 of the plugin).

$this->xslt_index = (empty($this->xslt)) ? '' : str_replace('.xsl', 'index.xsl', $this->xslt);

In regular terms, the left side just says if that setting is empty, use the default. But the right side says is something is put in the box, replace .xsl with index.xls and use that for the index style sheet. Remember how it changed my /sitemap.xsl-files/ to the non-existent /sitemapindex.xsl-files/ ? This changes every .xsl to index.xsl, even directories.

On the other hand, because it cannot find any .xsl in css-sitemap.php it makes no change at all. That creates an error on sitemap index pages because both the sitemap and sitemapindex pages use the same .xsl style sheet! Not good!


There is a simple solution to BOTH problems! In the file called class-bwp-simple-gxs.php, (around line 247 in version 1.2.2) replace make the change…


$this->xslt_index = (empty($this->xslt)) ? '' : str_replace('.xsl', 'index.xsl', $this->xslt);


$this->xslt_index = (empty($this->xslt)) ? '' : substr_replace($this->xslt, 'index', -4, 0);

In regular terms, what this says is … Start from the right, count four characters to the left (like: 1-“l“, 2-“s“, 3-“x“, 4-“.” OR 1-“p“, 2-“h“, 3-“p“, 4-“.“), and insert “index”!

This solves both problems at the same time. For example, for .xls …


  • http://alantait.com/sitemap.xsl-files/css-sitemap.xsl


  • http://alantait.com/sitemap.xsl-files/css-sitemapindex.xsl

Notice the directory does not change!  For .php …


  • http://alantait.com/sitemap.xsl-files/css-sitemap.php


  • http://alantait.com/sitemap.xsl-files/css-sitemapindex.php

So people who want to use PHP code in their sitemap style sheet may do so.

Adding the Domain Name

I can now use any PHP code I want in my style files:

  • css-sitemap.php
  • css-sitemapindex.php

Uppercase First Character of Domain Name

For example, to change my title and h1 tags in css-sitemap.php to include my domain name, with the fist character uppercase (a capital letter).


Alantait.com XML Sitemap

I can easily do this…

Change (around line 8):

<title>XML Sitemap</title>


<title><?= ucfirst( $_SERVER[ 'HTTP_HOST' ] ) ?> XML Sitemap</title>

Then Change (around line 54):

<h1>XML Sitemap</h1>


<h1><?= ucfirst( $_SERVER[ 'HTTP_HOST' ] ) ?> XML Sitemap</h1>

You could do the same thing in the css-sitemapindex.php file.

Uppercase Domain Name

Perhaps you would like the whole domain to be uppercase (capital letters). You could use this (this time in the css-sitemapindex.php file).



I can easily do this…


<title>XML Sitemap Index</title>


<title><?= strtoupper( $_SERVER[ 'HTTP_HOST' ] ) ?> XML Sitemap Index</title>

Then Change:

<h1>XML Sitemap Index</h1>


<h1><?= strtoupper( $_SERVER[ 'HTTP_HOST' ] ) ?> XML Sitemap Index</h1>

Again, you could do the same thing in the css-sitemap.php file.

Leave a Reply