7

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'); }
3
  • Please place the answer you discovered in an actual answer to this question rather than answering your question in the question itself.
    – bosco
    Feb 28 '14 at 3:34
  • Yes I will, but stackexchange wont let me answer my own question within 8 hours since created. I will update it tomorrow morning. Thanks @boscho
    – elvismdev
    Feb 28 '14 at 3:39
  • Ah, I was unaware of that restriction. Thanks in advance for moving it ;)
    – bosco
    Feb 28 '14 at 3:39
8
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
  • 1
    This wasn't exactly right for the question posed, but it's the closest solution. It provides a way to modify Yoast's robot command without hacking the plugin. In my case, I wanted to target a custom template and was able to do that with the following: add_filter('wpseo_robots', 'confPageNoIndex', 999); function confPageNoIndex($string= "") { if (is_page_template( 'page-confirmation.php') ) { $string= "no-index,no-follow"; } return $string; } This could be modified to target the search page. However, the Yoast plugin ALREADY no-indexes the Search Page.
    – heytricia
    Dec 11 '15 at 19:03
  • Thanks! This was the only solution that worked! Modified Snippet: ` add_filter('wpseo_robots', 'yoast_no_home_noindex', 999); function yoast_no_home_noindex($string= "") { if( is_paged() ) { $string= ""; } else { $Path = strtok($_SERVER["REQUEST_URI"],'?'); $URI="https://" . $_SERVER['HTTP_HOST'] .$Path; echo "<link rel=\"canonical\" href=\"" . $URI . "\">\n"; } return $string; } ` This determines if current URL is a paginated page and removes the robots tag. If its not, will display the canonical tag instead.
    – thamind
    Feb 22 '17 at 22:05
3

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' );
    }
}
0

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" />' );
}
4
  • Yes, I know this can be achieved this way, but I would like to not disable all of them by just one page, the site is using the Yoast SEO plugin that gives me full control and customization for SEO in the rest of the pages, less, this search page. In fact, having this, what i want to do is remove the meta created by wp_head and then output the same but with nofollow instead of follow. Some like str_replace('<meta name=robots content="noindex,follow"/>', '<meta name=robots content="noindex,nofollow"/>', wp_head());
    – elvismdev
    Feb 28 '14 at 1:17
  • I am sorry - I don't understand what you are attempting to achieve. The solution I provided will retain the tag on all pages except the search results page, just as the suggested code at the bottom of your question would. I am also not familiar with the Yoast SEO plugin, so I am afraid I cannot be of much help.
    – bosco
    Feb 28 '14 at 1:22
  • Please explain what you mean by, "I would like to not disable all of them by just one page"
    – bosco
    Feb 28 '14 at 1:36
  • If I check "Discourage search engines from indexing this site" I will have to manually output the meta-tag for all kind of pages, posts, tags. Yoast SEO plugin gives an array of options for this pages and I dont want to lose that. I just need to customize the output of this SEO meta-tag in the search result page, just this one.
    – elvismdev
    Feb 28 '14 at 1:43
0

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'; }
3
  • You should mark this answer as right answer.
    – Sisir
    Jun 29 '14 at 7:45
  • NOTE: With the current release of WordPress SEO this answer is no longer valid - the lines modified within the plugin no longer exist. Visitors to this question may find that my second answer provides solutions which do not require plugin hacks. If you are not using WordPress SEO or wish to remove search-engine indexing from every page, the best solution to the question as posed is to enable "Discourage search engines from indexing this site."
    – bosco
    Aug 31 '14 at 0:14
  • on the latest version of yoast plugin search for this code else { if ( is_search() || is_404() ) { $robots['index'] = 'noindex'; } it is on line 693, kindly edit/remove the code $robots['index'] = 'noindex'; that how I fixed mine. Mar 11 '19 at 4:22
-2

Is this what your looking for?

if ( is_search() ) {
    remove_action( 'do_robots', 'do_robots');
}
3
  • 2
    Please explain this answer and why you think it should solve the problem.
    – s_ha_dum
    Feb 28 '14 at 1:26
  • @Yarr you would mean if ( is_search() ) { remove_action( 'wp_head', 'do_robots'); } either way I just try this, but no removal of <meta name=robots content="noindex,follow"/>
    – elvismdev
    Feb 28 '14 at 1:32
  • 2
    The do_robots() function just displays the robots.txt file - it doesn't have any relevance to meta-tags.
    – bosco
    Feb 28 '14 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.