I need to rewrite some URLs to custom templates. For example, I have a page at domain.com/page
, and when a user clicks 'remove' on an element, it goes to domain.com/page/?remove=54
. I'd like it to rewrite to domain.com/page/remove/54
.
Help is appreciated, thank you in advance!
This exact code is untested, but it's derived from another instance of a similar situation that I did solve recently:
add_action( 'init', 'ss_permalinks' );
function ss_permalinks() {
add_rewrite_rule(
'page/remove/([^/]+)/?',
'index.php?pagename=page&service=$matches[1]',
'top'
);
}
add_filter( 'query_vars', 'ss_query_vars' );
function ss_query_vars( $query_vars ) {
$query_vars[] = 'removeid';
return $query_vars;
}
Re-save your permalink settings once after implementing. page
is the slug of the page to point to when the user access this URL (domain.com/page/remove/432
), and $matches[1]
should be the number after remove/
in the URL. This number is accessible by the variable specified later, $query_vars[] = 'removeid';
/ $removeid on the target page's template will be the number in the URL, if specified.