XML Sitemap without plugin.png

Generate XML Sitemap

For a clean and correct search engine optimization usually includes an XML sitemap. Surely this is not absolutely necessary for many small blogs, but usually an XML sitemap in WordPress makes sure that Google indexes posts much faster than without it.

With Sitemap it often only takes a moment, without it can take much longer until an article appears in the search results. This is interesting and good to know, but unfortunately sitemaps in WordPress are a thing of their own. SEO plugins that automatically create a sitemap are usually bloated heavyweights that slow down a blog too much. Also WordPress Sitemap Plugins are good, but not minimalistic enough to work really efficient.

So you need a solution without a plugin, a small snippet that automatically generates an XML sitemap when publishing or editing an article, or updates the existing one. No problem!

Add the following to the Functions.php of your theme:

add_action("publish_post", "create_sitemap"); add_action("publish_page", "create_sitemap"); function create_sitemap() { $postsForSitemap = get_posts( array( 'numberposts' => -1, 'orderby' => 'modified', 'post_type' => array( 'post', 'page' ), 'order' => 'DESC' ) ); $sitemap = ''; $sitemap .= "\n" . '' . "\n"; foreach( $postsForSitemap as $post ) { setup_postdata( $post ); $postdate = explode( " ", $post->post_modified ); $sitemap .= "\t" . '' . "\n" . "\t\t". '' . get_permalink( $post->ID ) . '' . "\n\t\t" . '' . $postdate[0] . '' . "\n\t\t\t". 'monthly' . "\n\t". ''. "\n"; } $sitemap .= ''; $fp = fopen( ABSPATH . "sitemap.xml", 'w' ); fwrite( $fp, $sitemap ); fclose( $fp ); }

XML Sitemap in WordPress

The XML Sitemap of WordPress, automatically generated by the code above, is valid and Google accepts it without any problems. With the minimal code, the XML Sitemap within WordPress is mercilessly performant, because a reasonable Sitemap can hardly be realized even more minimal and faster.

As soon as you publish a page or an article, or renew it with an update, the XML sitemap is automatically updated, which of course does not happen when you simply save an article. That would be feasible, but not very useful, because the sitemap should only be updated when new content is added and not constantly, because that would put too much load on the server.

In my case, this solution ran smoothly and flawlessly on blogs with several thousand articles. Google rated the sitemap as valid and it also complies with the Google Webmaster Guidelines and is accordingly honored by the search engine. A simple, effective and extremely minimalist solution to create an XML sitemap within WordPress, completely without plugin.

About Christian

ChristianMy name is Christian and I am co-founder of the platform fastWP. Here in the magazine I am responsible for the more "technical" topics but I like to write about SEO, which has been my passion for over 10 years now.

9 thoughts on “XML Sitemap (ohne Plugin)”

  1. Avatar

    Good to know! However, the sitemap about your snippet seems to only include new articles and pages. How can already existing posts be integrated? And what about Custom Post Types?

    1. Christian

      Hi Sascha, the snippet only creates a sitemap for new articles, so this is an option for new pages. Regarding your other two points, you'd have to write a much more advanced code that goes beyond the comments section here on the blog - what we "deliver" to readers here and what we don't 🙂

  2. Avatar

    can this be extended so that a new XML is also shot when a page goes offline? Otherwise there will be a page in the sitemap which does not exist and Google will not see this as a positive thing...

  3. Avatar

    I think I have rebuilt it so that the sitemap is shot whenever something is published or taken offline:

    function create_sitemap($new_status, $old_status, $post){
    if ( $old_status == $new_status || $old_status != 'publish' && $new_status != 'publish' )
    ...your XML code...
    add_action( 'transition_post_status', 'create_sitemap', 10, 3 );

  4. Avatar

    Hello, Christian,
    I need some more info to get the light XML Sitemap" working. Just what I need! Many thanks for this!
    Do I have to embed code into the file "./wp-includes/functions.php" (correct file and position of the code doesn't matter?) and simply call it up with "sitemap.xml" via a hyperlink? I don't quite understand how it works, as you can see.

    1. Avatar

      Hello, Conrad,
      in the child theme in the functions.php insert the above line. Then the sitemap will be added when a new page is created.
      As far as the original sitemap is concerned, I've consulted https://www.xml-sitemaps.com/ and moved the original site map into the root directory using Filiezilla.

Leave a Comment

Your email address will not be published. Required fields are marked *