How to Create a WordPress XML Sitemap Without a Plugin

How to Create a WordPress XML Sitemap Without a Plugin

Do you want to learn how to create a WordPress XML sitemap without a plugin?

An XML sitemap is a file where you provide information about the pages, videos, and other files on your site, and the relationships between them. In other words, XML sitemaps are made for search engines, not humans.

Even though it’s much easier to use a plugin to create an XML sitemap, some users prefer to create one themselves and are worried a plugin would slow down their site.

Let’s take a look at how to create an XML sitemap in WordPress without using a plugin. We’ll also cover the benefits of using a sitemap and show you a better way to create one.

Feel free to use our table of contents below to navigate through this jam-packed XML sitemap tutorial:

Benefits of Using an XML Sitemap

There are many benefits to using sitemaps. While sitemaps won’t directly boost your search engine rankings, they allow search engines to better crawl your website.

This means that they can find more content and start showing it in search results, which can result in higher rankings and more traffic to your site.

Without a sitemap, search engine bots can only find all your pages by following links, not post content, videos, archive pages, etc.

For this purpose, once you’ve started a WordPress website, you should make sure to create a sitemap so that search engines can discover, index, and crawl your content.

With that said, here are some benefits of using an XML sitemap on your WordPress site:

  • Informs Search Engines About Your Content  – an XML sitemap carries information about each page of your site, including when it was created and last modified, as well as its importance relative to other pages on your site.
  • Helps Search Engines Discover Your New Pages  – it tells search engines about your new pages and when you’ve published a new blog post on your site.
  • Increased Page and Crawl Priority – it changes the relative priority of pages on your website. For example, you can add a tag on your sitemap saying which pages are the most important and bots will focus on crawling these priority pages.
  • List Your Website’s URLs – you can submit a list of all URLs for your website. This is beneficial because some of the URLs may not have been found by search engines without submitting them in a sitemap. 

Let’s move on to look at what types of websites that need a sitemap.

What Type of Websites Need a Sitemap?

WordPress XML Sitemap without a plugin

According to Google most websites will benefit from having a sitemap.

They are especially helpful for:

  • Large sites
  • New sites that have few external links 
  • Sites with lots of content that’s not properly linked
  • Sites with a lot of media content

Like you probably understand by now, the majority of sites should use a sitemap. Without an XML Sitemap, it would take longer for your content to appear on or be removed from search engines.

WordPress Automatically Generates a Basic Sitemap

Before anything else, we need to clarify that for WordPress 5.5 and newer versions, WordPress automatically generates a sitemap. In this version, they introduced a feature that adds basic XML sitemaps functionality into WordPress core.

As a result, the WordPress sitemaps are very basic, and they also include anything you might have no-indexed, which can cause errors on your site.

For more details, you can check out our post on whether you should use a WordPress sitemap or create your own.  

Is It Best to Create an XML Sitemap With or Without a Plugin?

How to Create a WordPress XML Sitemap Without a Plugin

Plugins are a considerable feature in WordPress, which adds new functionality to your WordPress site. And yet, many users believe that the fewer plugins they use on their sites the better, but that’s not always necessarily true.

It’s true that badly coded plugins could add bloat and slow down your site. But a well-made, lightweight plugin can simplify things and save you a lot of time, without any ill effects.

In fact, the number of plugins on a WordPress site has actually next to no direct correlation with your site’s speed and performance. It’s all about the quality of the plugins installed. You can read more about how plugins affect your site’s speed here.

Before we get into things, let’s take a look at why creating a WordPress XML sitemap without a plugin may not be the best choice for your site.

  • It’s Difficult and Time Consuming – not using an SEO plugin to create your sitemap is difficult and takes a lot of extra time. You need to know how to use PHP code and be able to figure out how to code a functioning XML sitemap.
  • You May Lose All Your Hard Work – your changes will most likely be wiped off when you update your WordPress theme.
  • You Can’t Switch WordPress Theme – you can’t change your WordPress theme since your custom code will only work if you’re using that particular theme.
  • You’ll Miss Out on Powerful Features – with an SEO plugin, you can easily include advanced features such as video sitemaps, Google News sitemaps, granular sitemap control features, and so forth.

To conclude, if you want to make customization to your sitemap and also include advanced content, we recommend using the most powerful SEO plugins in the market, All in One SEO (AIOSEO).

You can feel totally certain knowing that this plugin is well optimized for speed and won’t slow your site down.

The Best WordPress SEO Plugin All in One SEO (AIOSEO)

Additionally, you get to choose to include or exclude:

  • Images and galleries
  • Password-protected posts
  • Custom post types
  • Media attachment pages
  • Archive pages for tags, categories, authors
  • and much more 

Best of all is that AIOSEO will automatically notify search engines whenever you publish, update, or delete content on your site.

With AIOSEO, you can make sure that Google knows which pages are the most important on your site, and only ranks your high-quality content.

Create a WordPress XML Sitemap Without a Plugin

With that said, if you still want to learn how to create a WordPress XML sitemap without a plugin, we’ll show you how to do that next. But for this, you’ll need to know how to use PHP code.

Note: When creating a WordPress XML sitemap without a plugin, you have to manually inform Google’s crawlers about the file. To do this, you have to submit the file to your Google Search Console (formerly Google Webmaster Tools).

For more details, please visit WPBeginner’s post on how to add your WordPress site to Google Search Console.

Now, let’s get into the nitty gritty of how to add a sitemap in WordPress.

The first step is to create a custom code for your XML sitemap. If you’re not used to PHP code and don’t know how to code an XML sitemap for WordPress, you can use our example below.

Custom Code for WordPress XML Sitemap 

Now, copy the custom code snippet for the XML sitemap, and add it to the functions.php file in your WordPress website. We’ll show you how to do that below.

/* function to create sitemap.xml file in root directory of site  */
// add_action("publish_post", "eg_create_sitemap");
// add_action("publish_page", "eg_create_sitemap");
add_action( "save_post", "eg_create_sitemap" );
function eg_create_sitemap() {
    if ( str_replace( '-', '', get_option( 'gmt_offset' ) ) < 10 ) { 
        $tempo = '-0' . str_replace( '-', '', get_option( 'gmt_offset' ) ); 
    } else { 
        $tempo = get_option( 'gmt_offset' ); 
    }
    if( strlen( $tempo ) == 3 ) { $tempo = $tempo . ':00'; }
    $postsForSitemap = get_posts( array(
        'numberposts' => -1,
        'orderby'     => 'modified',
        'post_type'   => array( 'post', 'page' ),
        'order'       => 'DESC'
    ) );
    $sitemap .= '<?xml version="1.0" encoding="UTF-8"?>' . '<?xml-stylesheet type="text/xsl" href="' . 
        esc_url( home_url( '/' ) ) . 'sitemap.xsl"?>';
    $sitemap .= "\n" . '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
    $sitemap .= "\t" . '<url>' . "\n" .
        "\t\t" . '<loc>' . esc_url( home_url( '/' ) ) . '</loc>' .
        "\n\t\t" . '<lastmod>' . date( "Y-m-d\TH:i:s", current_time( 'timestamp', 0 ) ) . $tempo . '</lastmod>' .
        "\n\t\t" . '<changefreq>daily</changefreq>' .
        "\n\t\t" . '<priority>1.0</priority>' .
        "\n\t" . '</url>' . "\n";
    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post);
        $postdate = explode( " ", $post->post_modified );
        $sitemap .= "\t" . '<url>' . "\n" .
            "\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' .
            "\n\t\t" . '<lastmod>' . $postdate[0] . 'T' . $postdate[1] . $tempo . '</lastmod>' .
            "\n\t\t" . '<changefreq>Weekly</changefreq>' .
            "\n\t\t" . '<priority>0.5</priority>' .
            "\n\t" . '</url>' . "\n";
    }
    $sitemap .= '</urlset>';
    $fp = fopen( ABSPATH . "sitemap.xml", 'w' );
    fwrite( $fp, $sitemap );
    fclose( $fp );
}

Note: Remember to ALWAYS use a Child Theme when editing the code. It allows you to easily update your parent theme without worrying about your theme crashing or losing any changes. For more details, please visit WPBeginner’s post on how to install a Child Theme in WordPress. 

After you’ve copied the custom code for the sitemap, you can add it by first navigating to Appearance » Theme Editor.

Once in Theme Editor, you need to open the functions.php file that you’ll find under Theme Files in the right column. 

WordPress theme editor

This will open a new window, where you can edit the file.

Next, paste in the custom code for the XML sitemap in the file editor. The instructions will tell you where to paste the code, but if they don’t, then you need to add the code at the bottom of the file.

Functions.php in WordPress theme editor

After you’ve added the code snippet, click on the Update File button.

An alternative is to use your FTP to add the custom code in theme files. Simply connect your FTP client to your website and go to /wp-content/themes/your-theme-folder/ and right click on the functions.php file.

Select the View/Edit file option to open the text editor. Once you’ve added the code, you click on Save.

You’ve now created a WordPress XML sitemap without a plugin. The sitemap will help search engines discover and index your content when you’ve published a new post. 

However, this is a basic sitemap and it only includes the parameter for publish_post. It doesn’t include other types of content, such as taxonomies, archive pages, video attachments, and so forth.

For that reason, and to make sure all content is included, we’ll show you a better way to create an XML sitemap.

A Better Way to Create an XML Sitemap

With AIOSEO, you can create an XML sitemap with just 1 click. The plugin is extremely user-friendly. You only need to install it and it’s all set!

If you’ve never installed a plugin before, you can find a step by step guide on how to install a WordPress plugin on WPBeginner.

Let’s show you how to use AIOSEO to create your XML sitemap.

After installing and activating the plugin, you simply visit Sitemaps in the All in One SEO menu.

Sitemaps menu item in All in One SEO

Next, you click on General Sitemap in the top main navigation menu.  

This will open the General Sitemap window, where you can see that Enable Sitemap is set to on.

Enable sitemap in All in One SEO

That’s it!

With a single click, AIOSEO is all set and will create an XML sitemap for you.

Better yet, it will automatically notify search engines whenever you publish, update, or delete content on your site.

It can’t get any easier than that!

Now, let’s move on to viewing your sitemap.

View Your Sitemap

If you want to view your sitemap, just click on the Open Sitemap button.

Open sitemap in All in One SEO

This will take you to an index page for your XML sitemap that looks like this:

Example of XML sitemap index page in All in One SEO

The index page lists all the sitemaps for your site, and each sitemap contains a different type of content.

For example, you can see there are different sitemaps for posts, pages, attachments, categories, etc.

Example of XML sitemap content in All in One SEO

Click on each WordPress sitemap URL in the index overview to view a list of that specific content.

Manually Test Your Sitemap

Finally, you can also manually test or view your sitemap by visiting your site’s URL and add /sitemap.xml in the end:

www.example.com/sitemap.xml

Your sitemap URL always displays the most recent version of your sitemap.

If you want to include more advanced content in your wordpress XML sitemap, you can find more details on how to customize your XML sitemap here.   

To summarize, sitemaps are important from a search engine optimization (SEO) perspective and indirectly help you improve your search engine rankings.

As a result, we highly recommend using sitemaps for your site and that you customize them to include all your important content.

We hope this blog post helped you learn how to create an XML sitemap in WordPress without a plugin, as well as shown you the enormous potential of using AIOSEO to also include advanced sitemap features.

If you’re still wondering whether it’s a good idea to create a WordPress XML sitemap without a plugin, you should check out our post on WordPress sitemaps vs AIOSEO.

And if you want to learn more about SEO, please also visit our ultimate WordPress SEO guide.

Stay tuned for AIOSEO’s new features and improvements.

Haven’t downloaded AIOSEO yet, what are you waiting for? 

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

This site uses Akismet to reduce spam. Learn how your comment data is processed.