Sitemaps are very important for the SEO of your Blog or Site. Sitemap mentioned the SE to use the mentioned URLs in their results. In short, a sitemap is the first step toward SEO. Nowadays webmasters first generate sitemap instead f writing some articles. So now you should also use sitemaps for your blog to be in the SE results and get organic traffic.
In WordPress, there are many plugins that are generation your sitemap automatically whenever you publish a page or post but you better know that plugins can cause a heavy load on your hosting and also make your blog unsecure. So it’s time to leave using plugins for sitemap in WordPress as we also hate WordPress plugins.
So here below we have some codes that will generate your WordPress posts and pages sitemap with stylesheet whenever you will publish a post or a page or update them. It will generate a real XML file in your hosting, not a fake one as plugins do.
Table of Contents
How To Make WordPress Sitemap Without Any Plugin?
Step 1:) Copy the below code and Paste in in your Theme Function(function.php) file.
/* ------------------------------------------------------------------------- * * WordPress Dynamic XML Sitemap Without Plugin * Codes By Emrah Gunduz & All In One SEO * Updated And Edited By EXEIdeas /* ------------------------------------------------------------------------- */ add_action("publish_post", "eg_create_sitemap"); add_action("publish_page", "eg_create_sitemap"); function eg_create_sitemap() { $postsForSitemap = get_posts(array( 'numberposts' => -1, 'orderby' => 'modified', 'post_type' => array('post','page'), 'order' => 'DESC' )); $sitemap = '<?xml version="1.0" encoding="UTF-8"?>'; $sitemap .= '<?xml-stylesheet type="text/xsl" href="sitemap-style.xsl"?>'; $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; foreach($postsForSitemap as $post) { setup_postdata($post); $postdate = explode(" ", $post->post_modified); $sitemap .= '<url>'. '<loc>'. get_permalink($post->ID) .'</loc>'. '<priority>1</priority>'. '<lastmod>'. $postdate[0] .'</lastmod>'. '<changefreq>daily</changefreq>'. '</url>'; } $sitemap .= '</urlset>'; $fp = fopen(ABSPATH . "sitemap.xml", 'w'); fwrite($fp, $sitemap); fclose($fp); }
Step 2:) Now Create a file named it as sitemap-style.xsl.
Step 3:) Now Copy the below code and Paste in that file and save it as above name.
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:html="http://www.w3.org/TR/REC-html40"xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"><xsl:variable name="fileType"> <xsl:choose> <xsl:when test="//sitemap:url">Sitemap</xsl:when> <xsl:otherwise>SitemapIndex</xsl:otherwise> </xsl:choose> </xsl:variable><html xmlns="http://www.w3.org/1999/xhtml"><head><title><xsl:choose><xsl:when test="$fileType='Sitemap'">Sitemap</xsl:when><xsl:otherwise>Sitemap Index</xsl:otherwise></xsl:choose></title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css">body {font-family:Helvetica,Arial,sans-serif;font-size:68.5%;}table {border:none;border-collapse:collapse;}table {font-size:1em;width:100%;}th {text-align:left;padding:5px;}tr.stripe {background-color:#f7f7f7;}</style></head> <body> <div id="content"> <h1>XML Sitemap By EXEIdeas</h1> <div> <p><xsl:choose><xsl:when test="$fileType='Sitemap'"> This sitemap contains <xsl:value-of select="count(sitemap:urlset/sitemap:url)"></xsl:value-of> URLs</xsl:when> <xsl:otherwise>This sitemap index contains <xsl:value-of select="count(sitemap:sitemapindex/sitemap:sitemap)"></xsl:value-of> sitemaps</xsl:otherwise></xsl:choose></p> </div><xsl:choose><xsl:when test="$fileType='Sitemap'"><xsl:call-template name="sitemapTable"/></xsl:when> <xsl:otherwise><xsl:call-template name="siteindexTable"/></xsl:otherwise></xsl:choose> </div> </body> </html> </xsl:template> <xsl:template name="siteindexTable"> <table cellpadding="3"><thead><tr><th width="50%">URL</th><th>LastChange</th></tr></thead><tbody><xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/><xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/><xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap"><tr><xsl:if test="position() mod 2 != 1"><xsl:attribute name="class">stripe</xsl:attribute></xsl:if><td><xsl:variable name="itemURL"><xsl:value-of select="sitemap:loc"/></xsl:variable><a href="{$itemURL}"><xsl:value-of select="sitemap:loc"/></a></td><td><xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/></td></tr></xsl:for-each></tbody></table> </xsl:template> <xsl:template name="sitemapTable"><table cellpadding="3"><thead><tr><th width="50%">URL</th><th>Priority</th><th>Change Frequency</th><th>LastChange</th></tr></thead><tbody><xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/><xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/><xsl:for-each select="sitemap:urlset/sitemap:url"><tr><xsl:if test="position() mod 2 != 1"><xsl:attribute name="class">stripe</xsl:attribute></xsl:if><td><xsl:variable name="itemURL"><xsl:value-of select="sitemap:loc"/></xsl:variable><a href="{$itemURL}"><xsl:value-of select="sitemap:loc"/></a></td><td><xsl:if test="string(number(sitemap:priority))!='NaN'"><xsl:value-of select="concat(sitemap:priority*100,'%')"/></xsl:if></td><td><xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat($lower, $upper),concat($upper, $lower)),substring(sitemap:changefreq, 2))"/></td><td><xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/></td></tr></xsl:for-each></tbody></table> </xsl:template></xsl:stylesheet>
Step 4:) Upload the above file to your WordPress hosting main folder.
Step 5:) Publish a new post or Update any old post.
Step 6:) Now open www.yourdomain.com/sitemap.xml
Step 7:) You will find out your sitemap there.
Customization:
1.) Change sitemap.xml with any of your desired name if you have a pre built sitemap at same name.
2.) Use same name at sitemap-style.xsl in both steps.
Last Words:
This is what we have and shared in easy steps for newbies so that they can easily know how it works. Stay with us because we are going to share a whole guide step by step about WordPress and make it easy for you. If you liked it then share it and be with us to get next tutorial. If you have any problem then feel free to ask us. We will help you with what we can or have.
Disclaimer: The basic code that we shared here is taken from Emrah Gunduz & All In One SEO. Rest we updated it with some more awesome features.
Hi Author,
That is just what we need, a clean sitemap.xml without having to use the Yoast Sitemap.xml.
I was curious to the output of your method in creating the sitemap, so I checked https://www.exeideas.com/sitemap.xml. I get a 404 response in stead of the sitemap.
Do you use the method? And how can certain pages and directories be excluded?
We are on At @https://www.exeideas.com/custom-sitemap.xml 🙂
Hi,
Thats looking clean 🙂
I will try this method, thanks!
PS: As a result of my hyperlinked name in the first comment I got nearly 400 backlinks because of the recent comment widget. Not good for SEO, I believe others also dislike it….. And I did not receive a message of your reply, the big increse in hyperlinks got me ending up here again 😉
Thanks for liking. Actually we have a lot of visitors that keep reading others comments so they always try ti check others. DO you want to delete it?
HI,
No do not delete my comment please.
From a SEO point of view this sidebar widget is linked to every page and gives a huge increase in links. Wile only the link on this post is important.
But never mind, all blogs do it like this.
Thanks for the method 🙂
Welcome here and thanks for reading our article and sharing your view.
Well if you can just clear my comment from the widget “recent comments” ? Or is that not possible without deleting my entire comment?
By the way you have a duplicated HP
https://www.exeideas.com/2013/08/new-xml-sitemap-for-blogspot.html
Thanks for mentioning our Bug. Recent comment is a widget so wait for others comments then your will be gone.
I made sitemap here http://share-ask.com/sitemap.xml as per your article
but it gives me below error at top
This XML file does not appear to have any style information associated with it. The document tree is shown below.
what should i do now
You followed half article. Follow the rest and add STYLE also.
Im getting now below error http://share-ask.com/sitemap-style.xsl
This page contains the following errors:
error on line 1 at column 113: attributes construct error
Below is a rendering of the page up to the first error.
Error 404 Not Found http://share-ask.com/sitemap.xml
Please feel free to contact me anytime via e-mail at besumitbe@gmail.com, or on Skype at bpshbp & be online to discuss
WE are happy to support you on email so be in touch with us there…