I’m trying to flush rewrite rules in a plugin and it isn’t working. I have to go to permalinks -> save in the admin panel for the rewrites to flush. Can anyone see why?
class SpektrixPlugin {
public function __construct(){
add_filter('query_vars', array($this, 'add_query_vars'));
add_action('init', array($this, 'add_rewrite_rules'));
}
public function activate() {
flush_rewrite_rules();
}
public function deactivate() {
flush_rewrite_rules();
}
public function add_query_vars($vars) {
$vars[] = "event"; // name of the var as seen in the URL
return $vars;
}
public function add_rewrite_rules() {
$page = get_page_by_title( 'Event' );
add_rewrite_rule( 'whats-on/(.+)/?$' , 'index.php?page_id='.$page->ID.'&event=$matches[1]' , 'top' );
}
}
$SpektrixEvents = new SpektrixPlugin;
register_activation_hook( __FILE__, array($SpektrixEvents, 'activate') );
register_deactivation_hook( __FILE__, array($SpektrixEvents, 'deactivate') );
Before flashing rules you have to add them: