Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to receive some special $_GET parameters for a custom page I'm making, i know how to receive this through a simple $_GET like mysite.net/products/?id=1 , but I wanted to use the URL style of my WordPress like mysite.net/products/1.

How to retrieve $_GET variables from rewritten URLs?

share|improve this question
    
I mistakenly close voted this as a duplicate... –  Stephen Harris Apr 10 '12 at 12:07
add comment

1 Answer

up vote 2 down vote accepted

To be added on init:

To register your custom variable ('id' in the question)

add_rewrite_tag('%mycustomvar%','([^&]+)');

To create a re-write rule:

add_rewrite_rule('^product/([0-9]{1,})/?','index.php?p=4&mycustomvar=$matches[1]','top')

4 is the id of the 'product' page. You will need to flush rewrite rules once after adding these (go to Permalink settings page)

You can get the value of mycustomvar: get_query_var( 'mycustomvar' ).

See Codex for:

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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