Disable Indexing of WordPress Archive Pages for Better SEO
Explanation
If you want to keep your WordPress archive pages out of search engine results, here's a simple way to do it.
Adding Noindex to Archive Pages:
There's a function that checks if a page is an archive. If it is, it adds a special tag in the page's head section. This tag tells search engines not to index the page, but they can still follow links on it. This helps keep your archive pages hidden from search results.
Updating the Robots.txt File:
The second part of the code modifies your site's robots.txt file. This file gives instructions to search engines about which parts of your site they should or shouldn't access. The code adds rules to block search engines from accessing common archive sections like categories, tags, authors, and dates. This further ensures these pages don't appear in search results.
By using both methods, you're effectively telling search engines to ignore your archive pages, keeping them out of search results while still allowing them to follow links for better navigation.
Code
<?php
// Function to add noindex to archive pages
function wp_dudecom_noindex_archives() {
if (is_archive()) {
echo '<meta name="robots" content="noindex, follow" />';
}
}
add_action('wp_head', 'wp_dudecom_noindex_archives');
// Function to modify robots.txt for archive pages
function wp_dudecom_modify_robots_txt($output, $public) {
if ('0' == $public) {
return $output;
}
$output .= "Disallow: /category/\n";
$output .= "Disallow: /tag/\n";
$output .= "Disallow: /author/\n";
$output .= "Disallow: /date/\n";
return $output;
}
add_filter('robots_txt', 'wp_dudecom_modify_robots_txt', 10, 2);
?>
Instructions
File Location: Add the code to your theme's functions.php
file or create a custom plugin file.
Prerequisites:
- Access to WordPress admin dashboard.
- Basic understanding of how to edit theme files or create a plugin.
Implementation Steps:
- Access WordPress Admin: Log in to your WordPress admin dashboard.
- Navigate to Theme Editor: Go to Appearance > Theme Editor. If you prefer using a plugin, navigate to Plugins > Add New and create a new plugin.
- Edit Functions File: In the Theme Editor, locate and click on
functions.php
from the list of theme files on the right. - Insert Code: Copy the provided code and paste it at the end of the
functions.php
file. If using a plugin, paste the code into the main plugin file. - Save Changes: Click the Update File button to save your changes.
- Verify Implementation: Visit an archive page on your site and check the page source to ensure the
<meta name="robots" content="noindex, follow" />
tag is present. - Check Robots.txt: Access your site's
robots.txt
file by navigating toyourdomain.com/robots.txt
and verify the disallow rules are added.
By following these steps, you can effectively prevent search engines from indexing your archive pages. If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.