Remove meta robots tag from wp_head

I am in need to remove just this line <meta name=robots content="noindex,follow"/> from wp_head but can’t find the right hook to use it with remove_action().

<meta name=robots content="noindex,follow"/>

Basically what I want to achieve is to remove just this line from the header but just for the search page. So in this case I would use something similar to:

if ( is_search() ) { remove_action('wp_head', 'whatever-the-action-name-is'); }

Related posts

5 comments

  1. add_filter('wpseo_robots', 'yoast_no_home_noindex', 999);
    function yoast_no_home_noindex($string= "") {
        if (is_home() || is_front_page()) {
            $string= "index,follow";
        }
        return $string;
    }
    

    this should be fine i think.. somewhere in your theme functions.php and should do the trick.

  2. Based off of your comments on my other answer implying that you explicitly wish to keep “Discourage search engines from indexing this site” enabled, after a more thorough investigation of WordPress core source (particularly default-filters.php), I think this is probably what you were after all along:

    add_action( 'posts_selection', 'indexSearchPage' );
    
    function indexSearchPage() {
        // Be sure to include the priority for the action or it won't be removed
        if( is_search() )
            remove_action( 'wp_head', 'noindex', 1 );
    }
    

    I use the posts_selection action hook as it’s the first hook in WordPress’s loading routine that has access to conditional tags. You can use later actions up to and including wp_head, but if you use wp_head itself you need to add the action with a priority less than 1 as noindex is added with a priority of 1:

    add_action( 'wp_head', 'indexSearchPage', -1 );
    

    Alternately, it is possible to conditionally trick WordPress into thinking that “Discourage search engines from indexing this site” is disabled:

    add_action( 'posts_selection', 'indexSearchPage' );
    
    function indexSearchPage() {
        if( is_search() ) {
            $alloptions = wp_load_alloptions();
            $alloptions[ 'blog_public' ] = '1';
            wp_cache_set( 'alloptions', $alloptions, 'options' );
            wp_cache_set( 'blog_public', '1', 'options' );
        }
    }
    
  3. I got it, the output is controlled by the WordPress SEO plugin as this is enabled in the site, so I had to tiny hard code in the plugin file class-frontend.php

    Obviously, I don’t like that much this dirty workaround but it works by now outputting that meta in the search page as I want.

    In the class-frontend.php plugin file I had to replace the line 552 by this

    if ( is_search() ) { $robots['follow'] = 'nofollow'; } else { $robots['follow'] = 'follow'; }
    
  4. I would recommend simply unchecking the “Discourage search engines from indexing this site” box from Settings > Reading on the dashboard (this should remove the robot-relevant meta-tags from all pages of your site), then manually adding the meta-tag back to your theme’s header using conditionals, like so:

    if( ! is_search() )
        echo( '<meta name="robots" content="noindex,nofollow" />' );
    

    You might have to alter the conditions to achieve your desired effect, but I think you get the idea.

    If you don’t care to alter your theme, you could attach it to the wp_head action hook:

    add_action( 'wp_head', 'noRobots' );
    
    function noRobots() {
        if( ! is_search() )
            echo( '<meta name="robots" content="noindex,nofollow" />' );
    }
    
  5. Is this what your looking for?

    if ( is_search() ) {
        remove_action( 'do_robots', 'do_robots');
    }
    

Comments are closed.