I wish to add a userq
parameter at the end of a URL with the username such as john
. I wish to change,
example.com/user/john
to,
example.com/user/index.php?userq=john
I tried this and this and they don't work.
add_action('init', 'user_profile_rewrite');
function user_profile_rewrite() {
global $wp, $wp_rewrite;
$wp->add_query_var('userq');
$rule = '^user/([^/]*)/?';
$rewrite = 'user/index.php?userq=$matches[1]';
add_rewrite_rule( $rule, $rewrite, 'top' );
$wp_rewrite->flush_rules();
}
I think the error has something to do with user/index.php?
so I tried removing the word user
by changing to index.php?...
but the URL remains like the original. How to solve this?
add_query_var
function. It's also very bad practice to flush rewrite rules on theinit
hook. Have you used one of the rewrite rule inspector plugins? The examples in the official documentation should work better than the answers you linked toindex.php?quervar=value&etc...
, they can't map to arbitrary files, and they can't be used to create redirects to other URLs. Rewrite rules also have a priority/order, but I can guarantee the way that you did it in your questions code, and the way the question/answers you linked to do it, are incorrect. A briefest of looks at the official documentation foradd_rewrite_rules
shows a far superior example that is safer, more reliable, and honestly easier to write