0

How can I create an xml sitemap without using any plugin?

<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <url>
            <loc>http://www.example.com/</loc>
            <lastmod>2005-01-01</lastmod>
            <changefreq>monthly</changefreq>
            <priority>0.8</priority>
        </url>
        <url>
            <loc>http://www.example.com/about.htm</loc>
            <changefreq>daily</changefreq>
        </url>
    </urlset>

How can I auto-generate a sitemap, I have 2000+ posts on my blog, and what is the method to add auto entry into sitemap when publishing a post. I have tried plugins but i want it manually without plugins like Google sitemap generate,

Rizier123
  • 58,877
  • 16
  • 101
  • 156
UI Xpider
  • 283
  • 1
  • 12

1 Answers1

0

Make a query to the DB and get all your posts and pages, then loop the results and create the sitemap like this:

before loop:

echo <<<LOB
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
LOB;

Inside the loop:

echo <<<LOB
<url>
<loc>$url</loc>
<lastmod>$date</lastmod>
<changefreq>$frequency</changefreq>
<priority>$priority</priority>
</url>
LOB;

after the loop:

echo "</urlset>";

Give the url of the script to google as the sitemap location.
Optionally, you can save the output of the script to a file.


You can also create the file robots.txt on the root of your website and declare the sitemap there, this way all spiders will now where your sitemap is.

robots.txt

Sitemap: http://yoursite.com/sitemap.php
IMSoP
  • 89,526
  • 13
  • 117
  • 169
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 1
    Libraries to do this (and doing it much better) already exists. This should be provided as an answer then. You're not even encoding the XML correctly, so this answer has technical flaws even. – hakre Apr 18 '15 at 15:09
  • Did read the same question I did? The SO clearly asks to "create an xml sitemap without using any plugin". My answer isn't complete because i never had the intention to do so, it's only a starting point. – Pedro Lobito Apr 18 '15 at 15:42
  • 1
    Yes I did, a Sitemap requires well-formed XML, you're not creating such. So the starting point is already flawed. – hakre Apr 18 '15 at 15:43
  • why don't you provide the answer you think is correct? – Pedro Lobito Apr 18 '15 at 15:45
  • I guess it's because that sometimes it's preferable to comment on existing material. But actually I don't think there is much reason at all. – hakre Apr 18 '15 at 15:48