If you want to rewrite example.com/test123
(a standard WordPress Page) to example.com/test234
(another standard WordPress Page) without having to save the rewrite rules in the database, then one option is using the request
filter hook:
add_filter( 'request', function ( $query_vars ) {
if ( isset( $query_vars['pagename'] ) ) {
$slug = $query_vars['pagename'];
// Define a list of source and target Page slugs.
$mapping = [
'test123' => 'test234',
'test567' => 'foo-bar',
//...
];
// If the requested slug is in the mapping list, change the requested page slug.
if ( ! empty( $mapping[ $slug ] ) ) {
$query_vars['pagename'] = $mapping[ $slug ];
}
}
return $query_vars;
} );
You can also use the parse_request
action hook, but the above should be fine, so I’m not including the code for that action hook.
But whether you use the filter hook or the action hook, the trick is to internally change the requested page slug if it matches the source slug in the mapping list/array. That way, requesting (or visiting) example.com/test123
is essentially the same as requesting example.com/test234
where the HTTP headers and page header, content, footer, etc. would be the same.