项目中需要一个好看的url,不想带参数。但是后台还要靠传递的参数来处理请求。
举例,要访问的url是http://local/site/sign-in/type1/
而处理请求时要将url视为http://local/site/sign-in/?source=type1

代码如下:
function custom_rewrite_rule()
{
    add_rewrite_rule('^sign-in/([^/]*)/?', 'index.php?page_id=188&source=$matches[1]', 'top'); //188为sign-in页面的ID
}
add_action('init', 'custom_rewrite_rule', 10, 0);

add_filter('query_vars', function ($vars) {
    $vars[] = "source";
    return $vars;
});

可在sign-in页面获取参数source的值:

get_query_var('source');
更多方法请参考:
https://developer.wordpress.org/reference/functions/add_rewrite_rule/