Getting add_rewrite_rule and add_rewrite_tag to work

I am new to using add_rewrite_rule and add_rewrite_tag. I am trying to get a very basic example to work, but nothing I do seems to make it work.

I want to access this URL (this works when entering this URl in directly):

http://localhost/?author_name=rewrite

Via this URL:

http://localhost/name/rewrite

I have the following code in functions.php:

$wp_rewrite->flush_rules();

add_action( 'init', 'addMyRules' );
function addMyRules(){
    add_rewrite_rule('^people/([^/]*)/?','index.php?author_name=$matches[1]','top');
    add_rewrite_tag('%author_name%','([^&]+)');
}

But I keep seeing a 404 page. Not sure what I am doing wrong.

1 Answer
1

I simply changed it to this, and it works!

add_action( 'init', 'addMyRules' );
function addMyRules(){
    add_rewrite_rule('^people/([^/]*)/?','index.php?author_name=$matches[1]','top');
    add_rewrite_tag('%author_name%','([^&]+)');
    flush_rewrite_rules();
}

Leave a Comment