How to Create Sitemap Manually for WordPress: Ultimate Guide
Key Takeaways:
- Generating XML sitemaps is crucial for search engine optimization (SEO) as it helps search engines understand the structure of your site.
- For WordPress users, creating an XML sitemap without relying on plugins is feasible and beneficial if you want to reduce plugin usage.
- Automate the process to include new posts and pages without manual updates.
A well-structured XML sitemap is a cornerstone of effective SEO for WordPress websites.
In this comprehensive guide, we'll walk you through every steps creating a robust XML sitemap manually without relying on plugins.
We'll cover sitemap for posts, pages, categories, and authors, enhancing search engine understanding and optimizing your site's visibility.
Step 1: Accessing Your WordPress Theme Files
- Log in to your WordPress dashboard.
- In the left-hand menu, click on "Appearance."
- From the drop-down, choose "Theme Editor."
Find your theme's functions.php file. This is where we'll insert the code for our XML sitemap.
Note for Beginners:
If you're using a default or pre-built WordPress theme, it's wise to use a child theme. Not sure how to set it up? No worries! Check out our guide on "How to Create a Child Theme" first to keep your changes safe during updates.
Step 2: Add Sitemap Registration Code
In functions.php, scroll down to the end of the file. add the following code to register custom sitemaps for posts, pages, categories, and authors:
// Register custom sitemaps
function custom_sitemaps() {
add_rewrite_rule('sitemap.xml$', 'index.php?sitemap=1', 'top');
add_rewrite_rule('sitemap-posts.xml$', 'index.php?sitemap=posts', 'top');
add_rewrite_rule('sitemap-pages.xml$', 'index.php?sitemap=pages', 'top');
add_rewrite_rule('sitemap-categories.xml$', 'index.php?sitemap=categories', 'top');
add_rewrite_rule('sitemap-authors.xml$', 'index.php?sitemap=authors', 'top');
}
add_action('init', 'custom_sitemaps');
Step 3: Create Sitemap Functionality
Now, let's add the functionality for generating sitemaps. Still in functions.php, scroll down further in the functions.php
file insert the following code:
// Generate custom sitemaps
function generate_custom_sitemap() {
if (isset($_GET['sitemap'])) {
header("Content-Type: text/xml");
switch ($_GET['sitemap']) {
case '1':
include('sitemap-index.php');
break;
case 'posts':
include('sitemap-posts.php');
break;
case 'pages':
include('sitemap-pages.php');
break;
case 'categories':
include('sitemap-categories.php');
break;
case 'authors':
include('sitemap-authors.php');
break;
}
exit;
}
}
add_action('template_redirect', 'generate_custom_sitemap');
Step 4: Create Sitemap Files
In this step, we'll guide you on where and how to create new files for each type of custom sitemap.
- Navigate to Your Theme Directory:
- Using an FTP client or the file manager in your hosting control panel, locate your WordPress installation directory.
- Inside it, find the "wp-content" folder.
- Access Your Theme Folder:
- Inside "wp-content," you'll find a folder named "themes" Open it.
- Within the "themes" folder, look for your active theme's folder (the one you're currently using).
- Create New Sitemap Files:
- Inside your active theme's folder, right-click and create new PHP files for each custom sitemap. For this guide, let's create
sitemap-index.php
,sitemap-posts.php
,sitemap-pages.php
,sitemap-categories.php
andsitemap-authors.php
.
- Inside your active theme's folder, right-click and create new PHP files for each custom sitemap. For this guide, let's create
Edit sitemap-index.php
file and insert following codes
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc><?php echo home_url('/'); ?>sitemap-posts.xml</loc>
</sitemap>
<sitemap>
<loc><?php echo home_url('/'); ?>sitemap-pages.xml</loc>
</sitemap>
</sitemapindex>
Edit sitemap-posts.php
file and insert following codes
sitemap-posts.php
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$posts = get_posts(array('posts_per_page' => -1));
foreach ($posts as $post) {
setup_postdata($post);
?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php echo get_the_modified_date('c', $post->ID); ?></lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<?php
}
wp_reset_postdata();
?>
</urlset>
Edit sitemap-pages.php
file and insert following codes
sitemap-pages.php
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$pages = get_pages();
foreach ($pages as $page) {
?>
<url>
<loc><?php echo get_page_link($page->ID); ?></loc>
<lastmod><?php echo get_the_modified_date('c', $page->ID); ?></lastmod>
<changefreq>weekly</changefreq>
<priority>0.7</priority>
</url>
<?php
}
?>
</urlset>
Edit sitemap-categories
.php
file and insert following codes
sitemap-categories
.php
// Include necessary XML header
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$categories = get_categories();
foreach ($categories as $category) {
?>
<url>
<loc><?php echo get_category_link($category->term_id); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
<?php
}
?>
</urlset>
Edit sitemap-authors
.php
file and insert following codes
sitemap-authors
.php
// Include necessary XML header
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$authors = get_users('who=authors');
foreach ($authors as $author) {
?>
<url>
<loc><?php echo get_author_posts_url($author->ID); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
<?php
}
?>
</urlset>
Step 5: Test Your Sitemap
Test the newly created sitemaps by visiting the following URLs:
- Main sitemap:
http://yoursite.com/sitemap.xml
- Post sitemap:
http://yoursite.com/sitemap-posts.xml
- Page sitemap:
http://yoursite.com/sitemap-pages.xml
- Categories sitemap:
http://yoursite.com/sitemap-categories.xml
- Authors sitemap:
http://yoursite.com/sitemap-authors.xml
Step 6: Submit Sitemap to Google Search Console
After creating sitemap you need to submit your sitemaps in google search console to tell google about your WordPress site's structure and index them.
By following this comprehensive guide, you've crafted a powerful XML sitemap tailored to your WordPress site's structure, all without the need for plugins. This meticulous approach, ensures that your site's content is accurately communicated to search engines, ultimately leading to improved SEO and increased visibility.
People also asked this but answer is same
FAQs on Manual XML Sitemap Creation for WordPress
Q: How do I manually add a sitemap to WordPress?
A: Begin by accessing your WordPress dashboard and navigating to "Appearance" > "Theme Editor." Ensure a secure process by implementing changes in a child theme. Our step-by-step guide provides detailed instructions for manual sitemap integration.
Q: How do I create a sitemap for WordPress?
A: Delve into our comprehensive guide, where we walk you through the intricacies of crafting a sitemap manually for your WordPress site. Understand the nuances of each step to harness the full SEO potential.
Q: How do I manually create a sitemap?
A: Tailor your sitemap to your WordPress site's unique structure with our guide's specific instructions. Learn how to handcraft a sitemap that ensures search engines grasp the nuances of your content hierarchy.
Q: How do I manually edit a sitemap in WordPress?
A: Navigate to the functions.php file in your child theme to make precise manual edits to your sitemap. Our guide elucidates this process, emphasizing the importance of maintaining theme integrity.
Q: How to create an HTML sitemap in WordPress without a plugin?
A: Explore our guide for an insightful, plugin-free approach to building an HTML sitemap in WordPress. Gain a nuanced understanding of the process and maintain a clean site structure.
Q: How do I add an HTML sitemap to WordPress?
A: Follow our specific steps for manual HTML sitemap integration into your WordPress site. Our guide ensures a meticulous process that aligns with your site's unique needs.
Q: How do I add a link to my WordPress sitemap?
A: Learn the art of manual link insertion to your sitemap in WordPress. Our guide provides specific insights into this process, allowing you to seamlessly integrate the link within your site.
Q: How do I submit an XML sitemap in WordPress?
A: After crafting your XML sitemap manually, refer to our guide for a specific submission process. Understand the intricacies of submitting it to search engines via their respective webmaster tools for effective indexing.
Q: How do I manually find the sitemap of a website?
A: Gain valuable insights into the specific steps for manually locating a website's sitemap. Our guide details the process, whether by appending "/sitemap.xml" to the domain or examining the robots.txt file.