You can create a rewrite rule using add_rewrite_rule()
that will match a given path to query parameters:
function wpse_283774_rewrite() {
add_rewrite_rule( '^events/([^/]+)/?', 'index.php?pagename=events&country=$matches[1]', 'top' );
}
add_action( 'init', 'wpse_283774_rewrite' );
This rule will match whatever is after /events
as the country
query parameter. Setting the third argument to top
means that it will match our rule first, otherwise it will try to match whatever is after /events
as a child page of /events
.
Now we just need to register country
as a valid query parameter:
function wpse_283774_query_vars( $vars ) {
$vars[] = 'country';
return $vars;
}
add_filter( 'query_vars', 'wpse_283774_query_vars' );
Now in your template/functions you can get whatever comes after /events
with get_query_var( 'country' )
:
if ( get_query_var( 'country' ) ) {
echo 'Events for ' . get_query_var( 'country' );
}
Just make sure to re-save your Permalinks settings after adding the code.