3

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

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();
}
1
  • 8
    you definitely do not want to flush rewrite rules on every request, it is a slow and expensive operation. rewrite rules should be flushed only when they change. please read the codex page for flush_rewrite_rules.
    – Milo
    Mar 31, 2014 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.