robots-template.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Robots template functions.
  4. *
  5. * @package WordPress
  6. * @subpackage Robots
  7. * @since 5.7.0
  8. */
  9. /**
  10. * Displays the robots meta tag as necessary.
  11. *
  12. * Gathers robots directives to include for the current context, using the
  13. * {@see 'wp_robots'} filter. The directives are then sanitized, and the
  14. * robots meta tag is output if there is at least one relevant directive.
  15. *
  16. * @since 5.7.0
  17. * @since 5.7.1 No longer prevents specific directives to occur together.
  18. */
  19. function wp_robots() {
  20. /**
  21. * Filters the directives to be included in the 'robots' meta tag.
  22. *
  23. * The meta tag will only be included as necessary.
  24. *
  25. * @since 5.7.0
  26. *
  27. * @param array $robots Associative array of directives. Every key must be the name of the directive, and the
  28. * corresponding value must either be a string to provide as value for the directive or a
  29. * boolean `true` if it is a boolean directive, i.e. without a value.
  30. */
  31. $robots = apply_filters( 'wp_robots', array() );
  32. $robots_strings = array();
  33. foreach ( $robots as $directive => $value ) {
  34. if ( is_string( $value ) ) {
  35. // If a string value, include it as value for the directive.
  36. $robots_strings[] = "{$directive}:{$value}";
  37. } elseif ( $value ) {
  38. // Otherwise, include the directive if it is truthy.
  39. $robots_strings[] = $directive;
  40. }
  41. }
  42. if ( empty( $robots_strings ) ) {
  43. return;
  44. }
  45. echo "<meta name='robots' content='" . esc_attr( implode( ', ', $robots_strings ) ) . "' />\n";
  46. }
  47. /**
  48. * Adds `noindex` to the robots meta tag if required by the site configuration.
  49. *
  50. * If a blog is marked as not being public then noindex will be output to
  51. * tell web robots not to index the page content. Add this to the
  52. * {@see 'wp_robots'} filter.
  53. *
  54. * Typical usage is as a {@see 'wp_robots'} callback:
  55. *
  56. * add_filter( 'wp_robots', 'wp_robots_noindex' );
  57. *
  58. * @since 5.7.0
  59. *
  60. * @see wp_robots_no_robots()
  61. *
  62. * @param array $robots Associative array of robots directives.
  63. * @return array Filtered robots directives.
  64. */
  65. function wp_robots_noindex( array $robots ) {
  66. if ( ! get_option( 'blog_public' ) ) {
  67. return wp_robots_no_robots( $robots );
  68. }
  69. return $robots;
  70. }
  71. /**
  72. * Adds `noindex` to the robots meta tag for embeds.
  73. *
  74. * Typical usage is as a {@see 'wp_robots'} callback:
  75. *
  76. * add_filter( 'wp_robots', 'wp_robots_noindex_embeds' );
  77. *
  78. * @since 5.7.0
  79. *
  80. * @see wp_robots_no_robots()
  81. *
  82. * @param array $robots Associative array of robots directives.
  83. * @return array Filtered robots directives.
  84. */
  85. function wp_robots_noindex_embeds( array $robots ) {
  86. if ( is_embed() ) {
  87. return wp_robots_no_robots( $robots );
  88. }
  89. return $robots;
  90. }
  91. /**
  92. * Adds `noindex` to the robots meta tag if a search is being performed.
  93. *
  94. * If a search is being performed then noindex will be output to
  95. * tell web robots not to index the page content. Add this to the
  96. * {@see 'wp_robots'} filter.
  97. *
  98. * Typical usage is as a {@see 'wp_robots'} callback:
  99. *
  100. * add_filter( 'wp_robots', 'wp_robots_noindex_search' );
  101. *
  102. * @since 5.7.0
  103. *
  104. * @see wp_robots_no_robots()
  105. *
  106. * @param array $robots Associative array of robots directives.
  107. * @return array Filtered robots directives.
  108. */
  109. function wp_robots_noindex_search( array $robots ) {
  110. if ( is_search() ) {
  111. return wp_robots_no_robots( $robots );
  112. }
  113. return $robots;
  114. }
  115. /**
  116. * Adds `noindex` to the robots meta tag.
  117. *
  118. * This directive tells web robots not to index the page content.
  119. *
  120. * Typical usage is as a {@see 'wp_robots'} callback:
  121. *
  122. * add_filter( 'wp_robots', 'wp_robots_no_robots' );
  123. *
  124. * @since 5.7.0
  125. *
  126. * @param array $robots Associative array of robots directives.
  127. * @return array Filtered robots directives.
  128. */
  129. function wp_robots_no_robots( array $robots ) {
  130. $robots['noindex'] = true;
  131. if ( get_option( 'blog_public' ) ) {
  132. $robots['follow'] = true;
  133. } else {
  134. $robots['nofollow'] = true;
  135. }
  136. return $robots;
  137. }
  138. /**
  139. * Adds `noindex` and `noarchive` to the robots meta tag.
  140. *
  141. * This directive tells web robots not to index or archive the page content and
  142. * is recommended to be used for sensitive pages.
  143. *
  144. * Typical usage is as a {@see 'wp_robots'} callback:
  145. *
  146. * add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
  147. *
  148. * @since 5.7.0
  149. *
  150. * @param array $robots Associative array of robots directives.
  151. * @return array Filtered robots directives.
  152. */
  153. function wp_robots_sensitive_page( array $robots ) {
  154. $robots['noindex'] = true;
  155. $robots['noarchive'] = true;
  156. return $robots;
  157. }
  158. /**
  159. * Adds `max-image-preview:large` to the robots meta tag.
  160. *
  161. * This directive tells web robots that large image previews are allowed to be
  162. * displayed, e.g. in search engines, unless the blog is marked as not being public.
  163. *
  164. * Typical usage is as a {@see 'wp_robots'} callback:
  165. *
  166. * add_filter( 'wp_robots', 'wp_robots_max_image_preview_large' );
  167. *
  168. * @since 5.7.0
  169. *
  170. * @param array $robots Associative array of robots directives.
  171. * @return array Filtered robots directives.
  172. */
  173. function wp_robots_max_image_preview_large( array $robots ) {
  174. if ( get_option( 'blog_public' ) ) {
  175. $robots['max-image-preview'] = 'large';
  176. }
  177. return $robots;
  178. }