Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create Google (Image) XML Sitemaps using PHP
header("Content-type: text/xml; charset=utf-8");
//create your XML document, using the namespaces
$urlset = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="" xmlns:image="" /><!--?xml version="1.0" encoding="UTF-8"?-->');
//iterate over your sites pages or whatever you like
foreach (getMyPagesOrWhatEver() as $item):
//add the page URL to the XML urlset
$url = $urlset->addChild('url');
$url->addChild('loc', $item->URL );
$url->addChild('lastmod', $item->LASTMOD );
$url->addChild('changefreq', 'daily');  //weekly etc.
$url->addChild('priority', '1.0');
//add an image
if ( $item->IMAGE ):
$image = $url->addChild('image:image', null, '');
$image->addChild('image:loc',$item->IMAGE->URL, '');
$image->addChild('image:caption',$item->IMAGE->ALT_OR_TITLE , '');
//add whitespaces to xml output (optional, of course)
$dom = new DomDocument();
$dom->formatOutput = true;
//output xml
echo $dom->saveXML();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.