Add Meta Noindex Tags to Specific WordPress Pages Easily

How to add noindex tag to wordpress page; Wordpress noindex specific pages; Add noindex meta tag wordpress; Noindex a page in wordpress; Wordpress prevent page from being indexed; How to noindex posts in wordpress; Wordpress noindex nofollow meta tag; Stop wordpress page from showing in search; Wordpress noindex tag tutorial; Set wordpress page to noindex;

Explanation

Want to stop certain pages from appearing in search results? This code snippet is your friend. It adds a noindex tag to specific pages or posts, telling search engines not to list them.

Here's how it works:

  • The code hooks into the wp_head action, which is a part of WordPress that lets you add extra stuff to the head section of your site.
  • It checks if you're viewing a single page or post using is_singular().
  • It grabs the ID of the current page or post with get_queried_object_id().
  • There's a list of post IDs you want to hide from search engines. You can replace 12, 34, 56 with the IDs of your choice.
  • If the current page's ID matches one in your list, it adds a <meta name="robots" content="noindex, nofollow" /> tag to the page's head section.

Just update the list with the IDs of the pages you want to hide, and you're good to go!

Code

<?php
// Hook into 'wp_head' to add the noindex meta tag to specific pages
add_action('wp_head', 'wp_dudecom_add_noindex_meta_tag');

function wp_dudecom_add_noindex_meta_tag() {
    // Check if we are on a single page or post
    if (is_singular()) {
        // Get the current post ID
        $post_id = get_queried_object_id();
        
        // Array of post IDs to noindex
        $noindex_post_ids = array(12, 34, 56); // Replace with your specific post IDs
        
        // Check if the current post ID is in the noindex array
        if (in_array($post_id, $noindex_post_ids)) {
            echo '<meta name="robots" content="noindex, nofollow" />';
        }
    }
}
?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites: No additional plugins or settings are required.

Implementation Steps:

  1. Access your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are editing the functions.php file, or go to Plugins > Editor if you are adding it to a custom plugin.
  3. Locate and open the functions.php file or your custom plugin file.
  4. Copy the provided code snippet and paste it at the end of the file.
  5. Identify the post IDs of the pages you want to hide from search engines. You can find these IDs by going to Posts or Pages in your WordPress dashboard and hovering over the title of the post/page. The ID will appear in the URL preview at the bottom of your browser.
  6. Replace the numbers 12, 34, 56 in the code with your specific post IDs.
  7. Save the changes to the file.
  8. Visit the pages you have added to the noindex list and check the page source to ensure the <meta name="robots" content="noindex, nofollow" /> tag is present in the head section.

If you need further assistance or want to explore more advanced functionalities, consider reaching out to the experts at wp-dude.com.