diff --git a/htdocs/wp-content/themes/lawcarta/functions.php b/htdocs/wp-content/themes/lawcarta/functions.php index aff19ac..138ee4c 100644 --- a/htdocs/wp-content/themes/lawcarta/functions.php +++ b/htdocs/wp-content/themes/lawcarta/functions.php @@ -289,3 +289,69 @@ function add_custom_class($classes=array(), $menu_item=false) { } add_filter('nav_menu_css_class', 'add_custom_class', 100, 2); +/** + * Add Disallow for some file types. + * Add "Disallow: /wp-login.php/\n". + * Remove "Allow: /wp-admin/admin-ajax.php\n". + * Calculate and add a "Sitemap:" link. + */ +add_filter( 'robots_txt', function( $output, $public ) { + /** + * If "Search engine visibility" is disabled, + * strongly tell all robots to go away. + */ + if ( '0' == $public || LAWCARTA_ENVIRONMENT_NAME != 'prod') { + $output = "User-agent: *\nDisallow: /\nDisallow: /*\nDisallow: /*?\n"; + } else { + /** + * Disallow some file types + */ + foreach( array( 'jpeg','jpg','gif','png','mp4','webm','woff','woff2','ttf','eot' ) as $ext ) { + $output .= "Disallow: /*.{$ext}$\n"; + } + + /** + * Get site path. + */ + $site_url = parse_url( site_url() ); + $path = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : ''; + + /** + * Add new disallow. + */ + $output .= "Disallow: $path/wp-login.php\n"; + + /** + * Remove line that allows robots to access AJAX interface. + */ + $robots = preg_replace( '/Allow: [^\0\s]*\/wp-admin\/admin-ajax\.php\n/', '', $output ); + + /** + * If no error occurred, replace $output with modified value. + */ + if ( ! is_null( robots ) ) { + $output = $robots; + } + + /** + * Remove line Sitemap. + */ + $robots = preg_replace( '/Sitemap: [^\0\s]*\n/', '', $output ); + + /** + * If no error occurred, replace $output with modified value. + */ + if ( ! is_null( robots ) ) { + $output = $robots; + } + + /** + * Calculate and add a "Sitemap:" link. + * Modify as needed. + */ + $output .= "Sitemap: {$site_url['scheme']}://{$site_url[ 'host' ]}/wp-sitemap.xml\n"; + } + + return $output; + +}, 99, 2 ); // Priority 99, Number of Arguments 2. \ No newline at end of file