Created
August 8, 2024 14:43
-
-
Save broskees/5fab22818cf6da06dbaa418f891839a1 to your computer and use it in GitHub Desktop.
Really Simple Robots.txt editor for WordPress (doesn't require DISALLOW_FILE_EDIT or DISALLOW_FILE_MODS to be false)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Robots.txt Additions | |
* Description: Add custom rules to robots.txt | |
* Version: 1.0 | |
* Author: Joseph Roberts | |
* Author URI: https://github.com/broskees | |
* License: MIT | |
* License URI: https://opensource.org/licenses/MIT | |
**/ | |
if (! defined('ABSPATH')) { | |
exit; | |
} | |
new class | |
{ | |
private const ROBOTS_TXT_OPENER = '### WP ROBOTS.TXT ADDITIONS ###'; | |
private const ROBOTS_TXT_CLOSER = '### END WP ROBOTS.TXT ADDITIONS ###'; | |
private const OPTION_NAME = 'robots_txt_additions'; | |
public function __construct() | |
{ | |
add_filter('robots_txt', [$this, 'robotsTxtFilter'], PHP_INT_MAX); | |
add_action('admin_init', [$this, 'registerSettings']); | |
} | |
public function robotsTxtFilter(string $output): string | |
{ | |
$additions = get_option(self::OPTION_NAME); | |
if (! $additions) { | |
return $output; | |
} | |
$openerPos = strpos($output, self::ROBOTS_TXT_OPENER); | |
$closerPos = strpos($output, self::ROBOTS_TXT_CLOSER); | |
if ($openerPos !== false && $closerPos !== false) { | |
$output = substr_replace( | |
$output, | |
$additions, | |
$openerPos + strlen(self::ROBOTS_TXT_OPENER), | |
$closerPos - $openerPos - strlen(self::ROBOTS_TXT_OPENER) | |
); | |
return $output; | |
} | |
$output .= "\n" . self::ROBOTS_TXT_OPENER . "\n"; | |
$output .= $additions . "\n"; | |
$output .= self::ROBOTS_TXT_CLOSER . "\n"; | |
return $output; | |
} | |
public function registerSettings() | |
{ | |
add_settings_section( | |
'robots_txt_additions_section', | |
'Robots.txt Additions', | |
'__return_false', | |
'reading' | |
); | |
add_settings_field( | |
self::OPTION_NAME, | |
'Custom Rules', | |
[$this, 'settingsField'], | |
'reading', | |
'robots_txt_additions_section' | |
); | |
register_setting('reading', self::OPTION_NAME); | |
} | |
public function settingsField() | |
{ | |
$additions = get_option(self::OPTION_NAME); | |
printf( | |
'<textarea id="%1$s" name="%1$s" rows="10" cols="50">%2$s</textarea><p class="description">%s</p>', | |
self::OPTION_NAME, | |
esc_textarea($additions), | |
'Enter your custom robots.txt rules here.' | |
); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installing with composer: