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( )`

Related posts

2 comments

  1. 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

  2. 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.

Comments are closed.