Create a Custom XML Sitemap in WordPress Without Plugins
Explanation
Creating a custom XML sitemap in WordPress can help search engines better understand your site's structure. Here's a simple way to do it without using a plugin.
Rewrite Rule: This code sets up a special URL for your sitemap. When someone visits yourwebsite.com/custom-sitemap.xml, WordPress knows to show the sitemap.
Query Variable: We add a custom query variable called custom_sitemap. This helps WordPress identify when to generate the sitemap.
Generate Sitemap: When the custom sitemap URL is accessed, this part of the code creates the XML file. It lists all your published posts and some custom URLs you might want to include. Each entry has details like the last modified date, how often it changes, and its importance.
- Posts: All published posts are included with their links and last modified dates.
- Custom URLs: You can add specific pages you want in the sitemap, like special landing pages.
Flush Rewrite Rules: This ensures that WordPress recognizes the new URL structure whenever you switch themes.
By using this code, you can manually create a sitemap that fits your needs, without relying on plugins. Just remember to update the custom URLs as needed!
Code
<?php
// Hook into the 'init' action to register the sitemap rewrite rule
add_action('init', 'wp_dudecom_add_sitemap_rewrite_rule');
function wp_dudecom_add_sitemap_rewrite_rule() {
add_rewrite_rule('^custom-sitemap\.xml$', 'index.php?custom_sitemap=1', 'top');
}
// Hook into 'query_vars' to add custom query variable
add_filter('query_vars', 'wp_dudecom_add_query_vars');
function wp_dudecom_add_query_vars($vars) {
$vars[] = 'custom_sitemap';
return $vars;
}
// Hook into 'template_redirect' to generate the sitemap
add_action('template_redirect', 'wp_dudecom_generate_custom_sitemap');
function wp_dudecom_generate_custom_sitemap() {
if (get_query_var('custom_sitemap')) {
header('Content-Type: application/xml; charset=utf-8');
$posts = get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => -1
));
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach ($posts as $post) {
$sitemap .= '<url>';
$sitemap .= '<loc>' . esc_url(get_permalink($post->ID)) . '</loc>';
$sitemap .= '<lastmod>' . esc_html(get_the_modified_date('c', $post->ID)) . '</lastmod>';
$sitemap .= '<changefreq>weekly</changefreq>';
$sitemap .= '<priority>0.8</priority>';
$sitemap .= '</url>';
}
// Add custom URLs
$custom_urls = array(
home_url('/custom-page-1/'),
home_url('/custom-page-2/')
);
foreach ($custom_urls as $url) {
$sitemap .= '<url>';
$sitemap .= '<loc>' . esc_url($url) . '</loc>';
$sitemap .= '<changefreq>monthly</changefreq>';
$sitemap .= '<priority>0.5</priority>';
$sitemap .= '</url>';
}
$sitemap .= '</urlset>';
echo $sitemap;
exit;
}
}
// Hook into 'after_switch_theme' to flush rewrite rules
add_action('after_switch_theme', 'wp_dudecom_flush_rewrite_rules');
function wp_dudecom_flush_rewrite_rules() {
flush_rewrite_rules();
}
?>
Instructions
To implement a custom XML sitemap in WordPress using the provided code, follow these steps:
File Location: Add the code to your theme's functions.php
file or create a custom plugin file.
Prerequisites: Ensure you have access to your WordPress theme files or the ability to create a custom plugin.
Implementation Steps:
- Access Your WordPress Files:
- Log in to your WordPress admin dashboard.
- Navigate to Appearance > Theme Editor if you're editing
functions.php
, or use an FTP client to access your WordPress files.
- Add the Code:
- Copy the provided code snippet.
- Paste it into the
functions.php
file of your active theme or create a new plugin file and paste the code there.
- Save Changes:
- If editing
functions.php
, click Update File to save your changes. - If using a custom plugin, ensure the plugin is activated in the Plugins section of your WordPress dashboard.
- If editing
- Flush Rewrite Rules:
- To ensure the new URL structure is recognized, go to Settings > Permalinks in your WordPress dashboard and click Save Changes.
- Verify the Sitemap:
- Visit yourwebsite.com/custom-sitemap.xml to check if the sitemap is generated correctly.
By following these steps, you can create a custom XML sitemap tailored to your site's needs. If you need further assistance or want to explore more advanced functionalities, consider reaching out to wp-dude.com for expert help.