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