charlie
charlie

Reputation: 481

wordpress add_rewrite_rule not rewriting

I have added this code to my themes functions.php file

function custom_rewrite_rule() {
    add_rewrite_rule('^shop/([^/]*)/?','index.php?page_id=1247&page=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 'top');

so when i browse to domain.com/shop/page1 it should rewrite to index.php?page_id=1247&page=page1 but when i do the following on page_id=1247 i get these results

echo $_GET["page"]; // = nothing
print_r($_GET) // = `Array( )`

Upvotes: 2

Views: 489

Answers (2)

Ali Akbar Azizi
Ali Akbar Azizi

Reputation: 3506

You should get parameters with $wp_query->query_vars

Here is an example

global $wp_query;
echo $wp_query->query_vars['id'];

UPDATE

You need use add_rewrite_tag

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

Also wordpress use page in query, so use another name

Upvotes: 1

mathielo
mathielo

Reputation: 6795

Have you flushed and regenerated the rewrite rules in the database? As stated by the docs:

IMPORTANT: Do not forget to flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

Whenever you add a new rewrite rule you need to click "Save Changes" in the Permalinks admin page (even though you're not changing anything in this page) to make sure Wordpress updates the rules in the database.

Upvotes: 0

Related Questions