Laravel & Php

How to create a sitemap in WordPress without a plugin


sitemap

An XML Sitemap, short for Extensible Markup Language Sitemap, serves as a document containing details about the pages, images, and various files present on your website, along with their interconnections. Search engines, such as Google, utilize this file to enhance the efficiency of their web crawling process. By providing a roadmap of your website’s structure, the XML Sitemap assists search engines in navigating your site more intelligently.

When search engines read the XML Sitemap, they gain insights into the pages and files you consider pertinent on your site. Additionally, the Sitemap furnishes valuable information about these items, including their last modification date, frequency of changes, and any alternative language versions available.

In essence, an XML Sitemap performs a crucial role in optimizing your website for search engines. It streamlines the task for search engine crawlers, facilitating the discovery and indexing of pages on your website. Even if your internal linking is not optimal, XML Sitemaps contribute positively to SEO by enabling Google to easily locate and prioritize your essential web pages.

This is how to make a WordPress Sitemap without the use of a plugin.

1. Open the theme’s functions.php file.

The functions.php file is where you incorporate your WordPress theme’s specific features. It can be used to integrate with WordPress’s core functions, making the theme more flexible, extensible, and efficient. So to start, open functions.php from your theme folder (wp-content/themes/your theme folder/functions.php) in your text editor.

In the right sidebar, Click Function.php

2. Copy and paste the code below into your theme’s function.php file.

/* ------------------------------------------------------------------------- *
* WordPress Dynamic XML Sitemap Without Plugin
/* ------------------------------------------------------------------------- */
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);
}

3. Now create a file named sitemap.xml

4. Copy and paste the code below into that file, then save it with the same 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>

5. Create a new post or update an existing one.

Now that you’ve added a new page or post to your blog, this code will delete the old content and replace it with new content in the original file. Check the sitemap at www.mydomain.com/sitemap.xml, where you’ll find a modified XML sitemap.



Sitemap WordPress
Share with Friends

Like this chef? Share with friends..