I'm using rewrite rules to add an optional parameter to a page. This page uses a specific page template which handles the implementation of an external data source.
add_rewrite_rule('^page/([0-9]+)/?', 'index.php?pagename=page&id=$matches[1]', 'top');
When the page is loaded, the template loads an index from the external source or, if an id is given, a specific object.
Since I'm developing a theme and don't content manage the website, I'd like to add the rewrite rule based on the page name the template is used on. The slug could also be changed by editors down the line, that's why I want to somewhat automate this.
I know that rewrite rules are added (and flushed) best after theme activation, so I'm wondering if there is a different approach I could take to achieve this.
Edit
It doesn't seem to be clear what I'm trying.
I want that a page template enables a page to have a virtual path component.
If the page link is /page
I want to be able to open /page/1513
or /page/test
and get 1513
or test
as query parameter.
add_rewrite_rule
on each page load but you flush the rules cache only after modifications