Home » How to Create a Sitemap in WordPress Without a Plugin

How to Create a Sitemap in WordPress Without a Plugin

Sitemaps involve all the information about your site’s posts, pages, videos, and media files and indicate their relationships with one another. They are useful for Search Engine crawlers, for example, Google to monitor your WordPress website more effectively. So, as you may guess, in this article we are going to reveal how to create a sitemap in WordPress without a plugin.


When Do I Need a Sitemap?

when-do-i-need-a-sitemap.jpg

If your site’s pages are linked correctly then Google will most likely crawl the biggest part of your website. This means that all the important pages will be navigated through the appropriate form. Even in this case, the sitemap features can be very effective. Yet, here are some cases where you definitely need a simple sitemap. Moreover, having this component on your website will somehow “invite” Search Engine to crawl your site.

  • Large Site: In this case, the users can crawl only a little part of your website pages. And they will most likely be the recently published post pages.
  • Isolated content pages that are not linked to one another: On this condition, you may mention these pages within the sitemap to rapidly index your site.
  • New website and a lack of external links: In the majority of cases, SE crawlers find this or that website page if they are linked to each other. This means that the external links play a significant role for Google to find your website.
  • There are many media files
  • Your site is shown on Google News Sitemap

In these two cases, Google can take the whole info from the sitemap and bring it to the search account.


Sitemap Types On Your Website

Before creating and implementing it on your WordPress site you need to pass certain steps. Here you may know which are the instructions:

  • Decide which format you want for your sitemap.
  • Choose the method of creating a sitemap. You may do it either manually or automatically.
  • Make it available for the Search Engine. This can be managed by adding it to your robots.txt file. Another way is to submit it into the Search Console.

Referring to the first point, you must know that there are different types of Sitemap formats:

  • XML
  • mRSS
  • RSS
  • Atom 1.0
  • Text
  • HTML sitemap

All these formats limit to a sitemap with 50 MB that is uncompressed and contains also 50000 URLs. On the condition, you want more URLs or have a media file with a larger size you need to split a single sitemap into multiple ones or simply create a sitemap index.


The XML Format

XML or Extensible Markup Language sets the encoding rules for the documents in a single format that is readable both for humans and automatic machines. Here you may see the format’s sitemap scheme:

  • In the beginning, you need to insert the <urlset> tag.
  • In the end, you should insert </urlset> tag.
  • The namespace must be specified within the <urlset> tag.
  • In the entry section add <url> as a parent XML tag for each of the entered URLs.
  • And for each of the <url> parent tags you must add a <loc> child entry.

Here you can see a single XML sitemap example:


<?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>

</urlset> 

The mRSS, RSS, Atom 1.0 formats

If your blog’s web feed is with RSS, mRSS, or Atom then you are able to submit the URL of the feed as a sitemap. There are 2 facts you have to know. First is that Google or generally the Search Engine may recognize RSS 2.0 and Atom 1.0 (this was created by the RSS developers as its alternative version). Also, you are able to utilize mRSS which is a media RSS. This enables you to give detailed info about your site video and media content.


The Text Format

If you are going to create a sitemap that involves only webpage URLs, then you may provide only a text file with a single URL per line. Yet, in this case, you must not include anything except for the URLs. For example:

Here are some points you should not ignore while creating a sitemap on WP.

  • Utilize qualified URLs.
  • Do not involve Session-Id from URL
  • Notify the SE about the availability of multilingual URLs.
  • Encode the sitemap index files (for, example, sitemap index.xml) with UTF-8.
  • Split the large sitemaps.
  • Involve only canonical URLs.

Creating a Sitemap On WordPress Site

In most cases, when you use a Content Management System like, for example, WordPress, it is more likely that the CMS has already created a sitemap for SE.

Yet, if it does not, then you need to create it yourself or ask a WordPress team to do it for you.

So, after revealing the best functionalities of sitemaps and their different types it is time to know how to create a sitemap in WordPress without a sitemaps plugin. At this point, we shall discuss the XML sitemaps.

Here is a step-by-step guide instruction that might be very helpful to you.

Step 1

First, you should enter the “functions.php” file where you save all your WP theme features. For this, you need to open it by entering the “Appearance“-“Theme Editor” sections and choosing “Theme functions” from the right side of the page.

how-to-create-sitemap-in-wordpress-without-a-plugin.jpg

wordpress-functions-php.jpg

Step 2

Continuing the action, you need to insert this code into your “functions.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 3

After pasting the code into the appropriate section you have to create a file and name it “sitemap-style.XSL“.

Step 4

Here is another code that should be pasted into the newly-created file. You must then save the file with the same name so that the new post or page replaces the old one.

<?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>

Submitting The XML Sitemap to Google

If you have successfully created your XML sitemap then you are able to send it to Search Engines. But in the first place, you need to verify the owner of the site through the Google Search Console, and after that upload your WP website sitemap into Google.

In order to check or view your sitemap, you are able to move to the Google Search Console and search your website. Then move to the “Index” and “Sitemaps” sections. Once you entered your sitemap URL in the appropriate field you must click on the “Submit” button. It will take a few days to crawl your pages and successfully proceed with your website sitemap submission.


Summing up

In conclusion, we would like you to note that the sitemaps can be very effective for your website. Since they are a unique type of “invitation” directed to the Search Engine. They all involve the most important pages or all the pages of your website. So if you want the Search Engine crawls your website effectively then it is high time you created a single sitemap for it.

So, in this article, we discussed how to insert a sitemap on your WP site manually without the help of any WordPress plugin. Yet, first of all, we informed you about the types of the sitemaps, such as:

  • XML
  • RSS, mRSS, Atom 1.0
  • Text

And after this, we created an XML sitemap manually using the mentioned codes.

So if you follow the said instructions and apply all the points noted in the step-by-step guide then you will definitely succeed even without the assistance of the developer.

If you like the article, do not hesitate to find us on Facebook and Twitter. For interesting WP tutorials, please subscribe to our YouTube channel.

2 thoughts on “How to Create a Sitemap in WordPress Without a Plugin”

    1. Anahit V.

      Hi, Armando!!
      Thanks for your kind feedback.
      I would like to inform you that sitemaps are not required for being found by the Search Engine. Yet, they are highly recommended. As sitemaps assist the crawlers to easily enter all the pages of your site.

Leave a Comment

Your email address will not be published.

Increase Your Traffic Now!

Want to get a book written by our SEO Specialist who shares his 30+ proven strategies with you?🔥
Enter your email address below and check your Inbox!📥

This will close in 0 seconds

Scroll to Top