The is_robots() WordPress PHP function checks if the current query is for the robots.txt file.
Usage
To use the function, simply call it within your theme or plugin file:
if (is_robots()) { // Do something if the current query is for the robots.txt file. }
Parameters
- None
More information
See WordPress Developer Resources: is_robots()
Examples
Display a message when the current query is for the robots.txt file
if (is_robots()) { echo 'The current query is for the robots.txt file.'; }
Add custom rules to the robots.txt file using the do_robots
action
function custom_robots_rules() { if (is_robots()) { echo "Disallow: /private/"; } } add_action('do_robots', 'custom_robots_rules');
Disable a plugin’s functionality on the robots.txt file
if (is_robots()) { remove_action('wp_head', 'plugin_function_name'); }
Set a different cache duration for the robots.txt file
function set_cache_duration($duration) { if (is_robots()) { return 3600 * 24; // 1 day } return $duration; } add_filter('wp_cache_time', 'set_cache_duration');
Redirect the robots.txt file to a custom location
function redirect_robots_txt() { if (is_robots()) { wp_redirect('/custom-robots.txt'); exit; } } add_action('template_redirect', 'redirect_robots_txt');