1

I want the page mysite.com/product to be accessed with the URL

mysite.com/product/productname instead of mysite.com/product?name=productname

I am trying to achieve this with the code in the plugins main file:

 add_rewrite_rule('^(product)/([^/]*)/?', 'product?name=$matches[1]','top');

but I am getting the error:

Fatal error: Call to a member function add_rule() on a non-object in /location/wp-includes/rewrite.php on line 21

Any suggestions?

1 Answer 1

4

You're calling it too early. Call it on the init hook or later, e.g.:

add_action( 'init', 'add_alexes_rules' );
function add_alexes_rules() {
    add_rewrite_rule('^(product)/([^/]*)/?', 'product?name=$matches[1]','top');
}
2
  • Thanks you were right, doesn't appear to be doing anything though as if I do var_dump($_GET) when I visit mysite.com/product/productname it returns any empty array but that's a new problem
    – Alex
    Commented May 14, 2014 at 8:18
  • looks like I need to use codex.wordpress.org/Rewrite_API/add_rewrite_tag
    – Alex
    Commented May 14, 2014 at 9:17

Your Answer

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

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