Make WordPress Core

01/21/2021 01:35:16 AM (3 years ago)

Robots: Introduce Robots API.

This changeset introduces a filter-based Robots API, providing central control over the robots meta tag.

  • Introduces wp_robots() function which should be called anywhere a robots meta tag should be included.
  • Introduces wp_robots filter which allows adding or modifying directives for the robots meta tag. The wp_robots() function is entirely filter-based, i.e. if no filter is added to wp_robots, no directives will be present, and therefore the entire robots meta tag will be omitted.
  • Introduces the following wp_robots filter functions which replace similar existing functions that were manually rendering a robots meta tag:
    • wp_robots_noindex() replaces noindex(), which has been deprecated.
    • wp_robots_no_robots() replaces wp_no_robots(), which has been deprecated.
    • wp_robots_sensitive_page() replaces wp_sensitive_page_meta(), which has been deprecated. Its rendering of the referrer meta tag has been moved to another new function wp_strict_cross_origin_referrer().

Migration to the new functions is straightforward. For example, a call to add_action( 'wp_head', 'wp_no_robots' ) should be replaced with add_filter( 'wp_robots', 'wp_robots_no_robots' ).

Plugins and themes that render their own robots meta tags are encouraged to switch to rely on the wp_robots filter in order to use the central management layer now provided by WordPress core.

Props adamsilverstein, flixos90, timothyblynjacobs, westonruter.
See #51511.

1 edited


  • trunk/src/wp-includes/functions.php

    r49941 r49992  
    16411641 * @since 2.1.0
    16421642 * @since 5.3.0 Remove the "Disallow: /" output if search engine visiblity is
    1643  *              discouraged in favor of robots meta HTML tag in wp_no_robots().
     1643 *              discouraged in favor of robots meta HTML tag via wp_robots_no_robots()
     1644 *              filter callback.
    16441645 */
    16451646function do_robots() {
    34923493    <meta name="viewport" content="width=device-width">
    34933494        <?php
    3494         if ( function_exists( 'wp_no_robots' ) ) {
    3495             wp_no_robots();
     3495        if ( function_exists( 'wp_robots' ) && function_exists( 'wp_robots_no_robots' ) && function_exists( 'add_filter' ) ) {
     3496            add_filter( 'wp_robots', 'wp_robots_no_robots' );
     3497            wp_robots();
    34963498        }
    34973499        ?>
Note: See TracChangeset for help on using the changeset viewer.