Skip to content

Instantly share code, notes, and snippets.

@lkraav
Created May 27, 2020 11:08
Show Gist options
  • Save lkraav/b1d304203038288b8217d8f6bed802d4 to your computer and use it in GitHub Desktop.
Save lkraav/b1d304203038288b8217d8f6bed802d4 to your computer and use it in GitHub Desktop.
Sometimes you don't need all stock rewrite rules
<?php
/*
Plugin Name: Less Rewrite Rules
Plugin URI: https://conversionready.com
Description: Less matching, more speed. Removes "attachment", "feed", "trackback" rewrites.
Author: Leho Kraav
Author URI: https://conversionready.com
Version: 2015.05.01
License: MIT
*/
register_activation_hook( __FILE__, [ "Plugin_Less_Rewrite_Rules", "activate" ] );
register_deactivation_hook( __FILE__, [ "Plugin_Less_Rewrite_Rules", "deactivate" ] );
final class Plugin_Less_Rewrite_Rules {
private static $instance;
private static $flag;
private static $match_rules = [ "attachment", "feed", "trackback" ];
function __construct() {
static::$flag = __CLASS__ . "_flush";
add_action( "wp_loaded", [ __CLASS__, "init" ] );
}
public static function activate() {
if ( ! get_option( static::$flag ) ) {
add_option( static::$flag, true );
}
}
# plugin does a full run before deactivation, must flush without filter
public static function deactivate() {
remove_filter( "rewrite_rules_array", [ __CLASS__, "less_rewrite_rules" ] );
flush_rewrite_rules();
}
# rewrite rules must be filtered at all times
public static function init() {
add_filter( "rewrite_rules_array", [ __CLASS__, "less_rewrite_rules" ] );
# http://www.andrezrv.com/2014/08/12/efficiently-flush-rewrite-rules-plugin-activation/
if ( get_option( static::$flag ) ) {
flush_rewrite_rules();
delete_option( static::$flag );
}
}
# https://codex.wordpress.org/Plugin_API/Filter_Reference/rewrite_rules_array
# ensure empty object cache, when debugging rewrite rule database issues
public static function less_rewrite_rules( $rules ) {
foreach ( $rules as $rule => $rewrite ) {
foreach ( static::$match_rules as $match ) {
if ( strpos( $rule, $match ) || strpos( $rewrite, $match ) ) {
unset( $rules[ $rule ] );
}
}
}
return $rules;
}
public static function get_instance() {
return self::$instance ? self::$instance : new self;
}
}
Plugin_Less_Rewrite_Rules::get_instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment