Created
October 20, 2012 17:04
-
-
Save gherkins/3924036 to your computer and use it in GitHub Desktop.
Create Google (Image) XML Sitemaps using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" /><!--?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, 'http://www.google.com/schemas/sitemap-image/1.1'); | |
$image->addChild('image:loc',$item->IMAGE->URL, 'http://www.google.com/schemas/sitemap-image/1.1'); | |
$image->addChild('image:caption',$item->IMAGE->ALT_OR_TITLE , 'http://www.google.com/schemas/sitemap-image/1.1'); | |
endif; | |
endforeach; | |
//add whitespaces to xml output (optional, of course) | |
$dom = new DomDocument(); | |
$dom->loadXML($urlset->asXML()); | |
$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
@gherkins Nice one, could you show the full php file ready to use?
https://ps.w.org/udinra-all-image-sitemap/assets/screenshot-3.png?rev=1632920