Home » How to create a dynamic XML Sitemap for wordpress

How to create a dynamic XML Sitemap for wordpress

Estimated Reading Time: 3 min

Rate this post

Creating a dynamic XML sitemap in WordPress without using a plugin involves creating a custom PHP file that generates the sitemap dynamically based on your site’s posts. Here’s how you can do it:


Steps to Create a Dynamic XML Sitemap

(Click to see – Sitemap for be-smart.blog )

1. Create the Sitemap File

  • Add a new file called sitemap.php to your theme’s root directory (or elsewhere on your server).

Example code for sitemap.php:

<?php
header("Content-Type: application/xml; charset=UTF-8");

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php
    // Load WordPress environment
    require_once('wp-load.php');

    $posts = get_posts([
        'numberposts' => -1,
        'post_type'   => 'post',
        'post_status' => 'publish',
    ]);

    foreach ($posts as $post) {
        $post_url = get_permalink($post->ID);
        $lastmod  = get_post_modified_time('Y-m-d\TH:i:sP', true, $post->ID);
        ?>
        <url>
            <loc><?php echo esc_url($post_url); ?></loc>
            <lastmod><?php echo esc_html($lastmod); ?></lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.8</priority>
        </url>
    <?php } ?>
</urlset>

2. Make the File Accessible

  • Ensure the sitemap.php file is publicly accessible by visiting https://yourdomain.com/sitemap.php.

3. Redirect to Sitemap via Rewrite Rules

  • WordPress expects the sitemap to be at https://yourdomain.com/sitemap.xml. Add a rewrite rule to redirect sitemap.xml requests to your sitemap.php. Add the following code to your functions.php file: add_action('init', function() { add_rewrite_rule('^sitemap\.xml$', 'sitemap.php', 'top'); }); add_action('template_redirect', function() { if (isset($_GET['sitemap'])) { include get_template_directory() . '/sitemap.php'; exit; } }); After adding this, refresh your rewrite rules by visiting Settings > Permalinks in the WordPress admin.

4. Submit Sitemap to Search Engines

  • Submit your sitemap URL (https://yourdomain.com/sitemap.php) to Google Search Console and other search engines.

Customizing the Sitemap

You can:

  • Add pages using get_pages() function.
  • Include custom post types by modifying the post_type parameter in get_posts().
  • Adjust <changefreq> and <priority> dynamically based on post types or categories.

Example for including pages and custom post types:

$pages = get_pages();
foreach ($pages as $page) {
    $page_url = get_permalink($page->ID);
    $lastmod  = get_post_modified_time('Y-m-d\TH:i:sP', true, $page->ID);
    ?>
    <url>
        <loc><?php echo esc_url($page_url); ?></loc>
        <lastmod><?php echo esc_html($lastmod); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
<?php }

Would you like a more advanced customization or help setting this up?


Discover more from Be-smart

Subscribe to get the latest posts sent to your email.

Photo of author

Flora

How to create a dynamic XML Sitemap for wordpress

Published

I am Flora, a www passionate dedicated to sharing insights and inspiration for living a fulfilling life. With a good background in www, I aim to empower others people to truly and genuinely acknowledge them when they do admirable things, big and small.

Leave a Reply