Create a Sitemap in Your WordPress Site Without Using a Plugin

In

As WordPress users, we often find ourselves relying heavily on plugins for various functionalities. But the use of too many plugins can cause multiple issues on your site such as site performance issue, security vulnerabilities issues and so on.

Today, we will create a sitemap for our website without a plugin. The details are below.

Step 1: Create Your Sitemap Generator File

The first step in creating a custom sitemap is to create a PHP script that generates your sitemap in XML format. This script will pull URLs from your WordPress posts and pages dynamically and will organize them into a structured sitemap.

Where to Create the File:

  1. Access cPanel: Log in to the cPanel of your hosting account.
  2. Open File Manager: Navigate to the File Manager, which allows you to manage all the files on your server.
  3. Locate the Root Directory: In the File Manager, find the root directory of your WordPress installation, typically named public_html.
  4. Inside the root directory, create a new PHP file. You might name it something like sitemap_generator.php. This is where you will paste the sitemap generation code.
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('wp-load.php');

header('Content-Type: application/xml; charset=utf-8');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// Query for pages
// This section adds all the pages to the sitemap
$page_args = array(
    'post_type' => 'page',
    'posts_per_page' => -1, // Retrieve all pages
);
$page_query = new WP_Query($page_args);

if ($page_query->have_posts()) {
    while ($page_query->have_posts()) {
        $page_query->the_post();
        echo '<url>' . "\n";
        echo '<loc>' . esc_url(get_permalink()) . '</loc>' . "\n";
        echo '<lastmod>' . get_the_modified_date('c') . '</lastmod>' . "\n";
        echo '<changefreq>monthly</changefreq>' . "\n";
        echo '<priority>0.6</priority>' . "\n";
        echo '</url>' . "\n";
    }
}

// Reset post data
wp_reset_postdata();

// Query for posts
// This section adds all the blog posts to the sitemap
$post_args = array(
    'post_type' => 'post',
    'posts_per_page' => -1, // Retrieve all posts
);
$post_query = new WP_Query($post_args);

if ($post_query->have_posts()) {
    while ($post_query->have_posts()) {
        $post_query->the_post();
        echo '<url>' . "\n";
        echo '<loc>' . esc_url(get_permalink()) . '</loc>' . "\n";
        echo '<lastmod>' . get_the_modified_date('c') . '</lastmod>' . "\n";
        echo '<changefreq>weekly</changefreq>' . "\n";
        echo '<priority>0.5</priority>' . "\n";
        echo '</url>' . "\n";
    }
}

echo '</urlset>';

// Reset post data
wp_reset_postdata();
?>

Step 2: Configure the .htaccess File for the Correct Sitemap URL

Understanding .htaccess: The .htaccess file is a powerful configuration file used by Apache web servers. It allows you to set server-level directives for your website, including URL redirections, security enhancements, and performance tweaks.

Configuring .htaccess for Your Sitemap: To ensure your sitemap is accessible at the desired URL (like https://yourwebsite.com/sitemap.xml), you need to set up a rewrite rule in the .htaccess file. This step involves telling the server to process requests for sitemap.xml using your PHP script.

  1. Locate or Create the .htaccess File:
  • In cPanel’s File Manager, go to the root directory of your WordPress site (public_html).
  • Look for the .htaccess file. If it doesn’t exist, you can create a new one.

2. Edit the .htaccess File:

  • Open the .htaccess file for editing.
  • Paste the following code before #BEGIN WordPress:
RewriteEngine On
RewriteRule ^sitemap\.xml$ /sitemap_generator.php [L]

Save the Changes:

After adding the code, save your changes. Now you can view the sitemap using the following URL https://your-site.com/sitemap.xml


Comments

Leave a Reply

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