I have created pages with slug test123
and test234
in wordpress, I want to create a plugin that doing the following
- when user request
test123
, and the content intest234
will be served instead.
I understand I can use the rewrite API but it is possible to do so without the need of flush_rewrite_rules
? That is, without setting the rules in database?
I have created pages with slug test123
and test234
in wordpress, I want to create a plugin that doing the following
- when user request
test123
, and the content intest234
will be served instead.
I understand I can use the rewrite API but it is possible to do so without the need of flush_rewrite_rules
? That is, without setting the rules in database?
1 Answer
Reset to default 7 +50If you want to rewrite example/test123
(a standard WordPress Page) to example/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/test123
is essentially the same as requesting example/test234
where the HTTP headers and page header, content, footer, etc. would be the same.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745050706a4608369.html
test123
page to display the post content of thetest234
page, you could simply use a shortcode or edit the page template to query and display thetest234
content? Why do you need the URL rewrite? – Sally CJ Commented Oct 16, 2019 at 15:02request
orparse_request
hook.. – Sally CJ Commented Oct 28, 2019 at 8:00