Make WordPress Core


Ignore:
Timestamp:
01/21/2021 01:35:16 AM (4 years ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/deprecated.php

    r49597 r49992  
    41364136    return is_string( $value ) ? addslashes( $value ) : $value;
    41374137}
     4138
     4139/**
     4140 * Displays a noindex meta tag if required by the blog configuration.
     4141 *
     4142 * If a blog is marked as not being public then the noindex meta tag will be
     4143 * output to tell web robots not to index the page content. Add this to the
     4144 * {@see 'wp_head'} action.
     4145 *
     4146 * Typical usage is as a {@see 'wp_head'} callback:
     4147 *
     4148 *     add_action( 'wp_head', 'noindex' );
     4149 *
     4150 * @see wp_no_robots()
     4151 *
     4152 * @since 2.1.0
     4153 * @deprecated 5.7.0 Use wp_robots_noindex() instead on 'wp_robots' filter.
     4154 */
     4155function noindex() {
     4156    _deprecated_function( __FUNCTION__, '5.7.0', 'wp_robots_noindex()' );
     4157
     4158    // If the blog is not public, tell robots to go away.
     4159    if ( '0' == get_option( 'blog_public' ) ) {
     4160        wp_no_robots();
     4161    }
     4162}
     4163
     4164/**
     4165 * Display a noindex meta tag.
     4166 *
     4167 * Outputs a noindex meta tag that tells web robots not to index the page content.
     4168 * Typical usage is as a {@see 'wp_head'} callback. add_action( 'wp_head', 'wp_no_robots' );
     4169 *
     4170 * @since 3.3.0
     4171 * @since 5.3.0 Echo "noindex,nofollow" if search engine visibility is discouraged.
     4172 * @deprecated 5.7.0 Use wp_robots_no_robots() instead on 'wp_robots' filter.
     4173 */
     4174function wp_no_robots() {
     4175    _deprecated_function( __FUNCTION__, '5.7.0', 'wp_robots_no_robots()' );
     4176
     4177    if ( get_option( 'blog_public' ) ) {
     4178        echo "<meta name='robots' content='noindex,follow' />\n";
     4179        return;
     4180    }
     4181
     4182    echo "<meta name='robots' content='noindex,nofollow' />\n";
     4183}
     4184
     4185/**
     4186 * Display a noindex,noarchive meta tag and referrer origin-when-cross-origin meta tag.
     4187 *
     4188 * Outputs a noindex,noarchive meta tag that tells web robots not to index or cache the page content.
     4189 * Outputs a referrer origin-when-cross-origin meta tag that tells the browser not to send the full
     4190 * url as a referrer to other sites when cross-origin assets are loaded.
     4191 *
     4192 * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_sensitive_page_meta' );
     4193 *
     4194 * @since 5.0.1
     4195 * @deprecated 5.7.0 Use wp_robots_sensitive_page() instead on 'wp_robots' filter
     4196 *                   and wp_strict_cross_origin_referrer() on 'wp_head' action.
     4197 */
     4198function wp_sensitive_page_meta() {
     4199    _deprecated_function( __FUNCTION__, '5.7.0', 'wp_robots_sensitive_page()' );
     4200
     4201    ?>
     4202    <meta name='robots' content='noindex,noarchive' />
     4203    <?php
     4204    wp_strict_cross_origin_referrer();
     4205}
Note: See TracChangeset for help on using the changeset viewer.