Here is an example of adding custom rewrite rule to WordPress.
For instance, you have a link
http://website.com/1/
that would like WordPress interprets internally as
http://website.com?tl=1
But for tl
you have mentioned, I am not sure what is your settings. So I assume tl
is working in your system. Because WP default parameter is p
for page id
You may try to use the following filter in your theme functions.php or plugin
- The following only works if permalinks is turned on. Otherwise, no effect.
It means insettings -> permalinks
, if you choosePlain
, then WordPress will show the query without pretty URL. It is regarded asturned off
.
Otherwise, if you choose other options likePost Name
then WordPress will accept Post Name as URL such ashttps://examples.com/sample-post/
So, if it is being enabled(or turned on). Then the the rewrite system will be used. And the following filter works with WordPress rewrite system.
Sometimes we use some other synonym
like Enable permalinks
, Disable permalinks
. It also means the same things.
Here, assume to place in functions.php
add_action( 'init', 'q363603_add_custom_rewrites' );
function q363603_add_custom_rewrites() {
// take numbers to the query tl=numbers
add_rewrite_rule('([0-9])+/?$', 'index.php?tl=$matches[1]', 'top');
}
add_filter('query_vars', 'q363603_add_custom_queryvars' );
function q363603_add_custom_queryvars( $qvars ) {
// ask WordPress to allow this query parameter
$qvars[] = 'tl';
return $qvars;
}
edited: add permalinks turning on/off explanation