Instead of adding query_vars
with add_rewrite_tag()
, use the query_vars
filter hook. I’ve used the following code to test your routing and it’s working just fine.
class OP_Plugin {
public function init() {
add_action( 'init', array( $this, 'add_rewrite_rules' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
add_filter( 'template_include', array( $this, 'add_template' ) );
}
public function add_template( $template ) {
$account_page = get_query_var( 'account_page' );
if ( $account_page ) {
echo "Working! \n";
echo "Query: {$account_page}";
// return CUSTOMER_CAR_PLUGIN_DIR.'pages/customer-car-details.php';
return '';
}
return $template;
}
public function flush_rules() {
$this->rewrite_rules();
flush_rewrite_rules();
}
public function add_rewrite_rules() {
add_rewrite_rule( 'account/(.+?)/?$', 'index.php?account_page=$matches[1]', 'top' );
}
public function add_query_vars( $vars ) {
$vars[] = 'account_page';
return $vars;
}
}
$op_plugin = new OP_Plugin();
$op_plugin->init();