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/general-template.php

    r49977 r49992  
    31923192
    31933193/**
    3194  * Displays a noindex meta tag if required by the blog configuration.
    3195  *
    3196  * If a blog is marked as not being public then the noindex meta tag will be
    3197  * output to tell web robots not to index the page content. Add this to the
    3198  * {@see 'wp_head'} action.
    3199  *
    3200  * Typical usage is as a {@see 'wp_head'} callback:
    3201  *
    3202  *     add_action( 'wp_head', 'noindex' );
    3203  *
    3204  * @see wp_no_robots()
    3205  *
    3206  * @since 2.1.0
    3207  */
    3208 function noindex() {
    3209     // If the blog is not public, tell robots to go away.
    3210     if ( '0' == get_option( 'blog_public' ) ) {
    3211         wp_no_robots();
    3212     }
    3213 }
    3214 
    3215 /**
    3216  * Display a noindex meta tag.
    3217  *
    3218  * Outputs a noindex meta tag that tells web robots not to index the page content.
    3219  * Typical usage is as a {@see 'wp_head'} callback. add_action( 'wp_head', 'wp_no_robots' );
    3220  *
    3221  * @since 3.3.0
    3222  * @since 5.3.0 Echo "noindex,nofollow" if search engine visibility is discouraged.
    3223  */
    3224 function wp_no_robots() {
    3225     if ( get_option( 'blog_public' ) ) {
    3226         echo "<meta name='robots' content='noindex,follow' />\n";
    3227         return;
    3228     }
    3229 
    3230     echo "<meta name='robots' content='noindex,nofollow' />\n";
    3231 }
    3232 
    3233 /**
    3234  * Display a noindex,noarchive meta tag and referrer origin-when-cross-origin meta tag.
    3235  *
    3236  * Outputs a noindex,noarchive meta tag that tells web robots not to index or cache the page content.
     3194 * Displays a referrer strict-origin-when-cross-origin meta tag.
     3195 *
    32373196 * Outputs a referrer origin-when-cross-origin meta tag that tells the browser not to send the full
    32383197 * url as a referrer to other sites when cross-origin assets are loaded.
    32393198 *
    3240  * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_sensitive_page_meta' );
    3241  *
    3242  * @since 5.0.1
    3243  */
    3244 function wp_sensitive_page_meta() {
     3199 * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_strict_cross_origin_referrer' );
     3200 *
     3201 * @since 5.7.0
     3202 */
     3203function wp_strict_cross_origin_referrer() {
    32453204    ?>
    3246     <meta name='robots' content='noindex,noarchive' />
    32473205    <meta name='referrer' content='strict-origin-when-cross-origin' />
    32483206    <?php
Note: See TracChangeset for help on using the changeset viewer.