| 52 | /** |
| 53 | * Returns the necessary X-Robots-Tag value. |
| 54 | * |
| 55 | * Gathers robots directives to include for the current context, using the |
| 56 | * {@see 'wp_robots'} filter. The directives are then sanitized, and the |
| 57 | * X-Robots-Tag header is output if there is at least one relevant directive. |
| 58 | * |
| 59 | * @since x.x.x |
| 60 | * |
| 61 | * @return string|null The X-Robots-Tag header value, or null if no relevant directives. |
| 62 | */ |
| 63 | function wp_x_robots_tag_header_string() { |
| 64 | /** This filter is documented in wp-includes/robots-template.php */ |
| 65 | $robots = apply_filters( 'wp_robots', array() ); |
| 66 | |
| 67 | $robots_strings = array(); |
| 68 | foreach ( $robots as $directive => $value ) { |
| 69 | if ( is_string( $value ) ) { |
| 70 | // If a string value, include it as value for the directive. |
| 71 | $robots_strings[] = "{$directive}={$value}"; |
| 72 | } elseif ( $value ) { |
| 73 | // Otherwise, include the directive if it is truthy. |
| 74 | $robots_strings[] = $directive; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if ( empty( $robots_strings ) ) { |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | return implode( ', ', $robots_strings ); |
| 83 | } |
| 84 | |