• wapoxac

    (@wapoxac)


    Hi,

    I am trying to add a “noindex, follow” to paginated pages, and tried adding the following function to functions.php, but the paginated pages still had “index, follow”.

    function is_paged() {
        <meta name="robots" content="noindex,follow" />
    }
    

    How can I get this to work preferably without using a plug-in. My function may not be correct, as I’m not a PHP developer.

    • This topic was modified 5 years ago by wapoxac.
Viewing 4 replies - 1 through 4 (of 4 total)
  • corrinarusso

    (@corrinarusso)

    The issue here is having this custom function over-write the existing values if a meta name robots index tag has already been inserted into the page.
    You would need to actually add a hook into native head function.

    https://developer.ww.wp.xz.cn/reference/functions/is_paged/

    Try this in your child theme:

    add_action('wp_head', 'meta_fix', 1);
    function meta_fix() {
      if(is_paged()) { echo "<meta name='robots' content='noindex, follow'/>"; }
    }
    Thread Starter wapoxac

    (@wapoxac)

    Thanks for sharing your expertise Corrinarusso. That worked great!

    There probably wouldn’t be a way to remove the original robots meta tag on just the paginated pages? So it doesn’t look this this where the original is listed first and then the injected afterwards?

    <meta name='robots' content='index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1' />
    <meta name='robots' content='noindex, follow'/>

    For anyone else who might benefit from this solution and know as little as me about PHP. I wrapped the code above in a PHP tag and put it in the functions.php file in WordPress > Appearance > Theme Editor > functions.php.

    So it looks like this:

    <?php 
    add_action('wp_head', 'meta_fix', 1);
    function meta_fix() {
      if(is_paged()) { echo "<meta name='robots' content='noindex, follow'/>"; }
    }
    ?>

    Remember to use a child-theme.

    • This reply was modified 5 years ago by wapoxac.
    • This reply was modified 5 years ago by wapoxac.
    Dion

    (@diondesigns)

    For WordPress 5.7+, the following should do what you want:

    function no_robots_paged($robots) {
    	if (is_paged()) {
    		$robots['index'] = $robots['follow'] = false;
    		$robots['noindex'] = $robots['nofollow'] = true;
    	}
    
    	return $robots;
    }
    add_filter('wp_robots', 'no_robots_paged', 999);
    Thread Starter wapoxac

    (@wapoxac)

    Please disregard wrapping it in the PHP tags above and instead follow corrinarusso’s suggestion above. I managed to get a “Cookies are blocked due to unexpected output” error when I attempted to log in to WordPress and had to edit the functions.php via FTP to get access again.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Adding custom function() not working’ is closed to new replies.