WordPress add_rewrite_rule can’t get inside matches[1]

I wrote a rewrite rule in WordPress but it can’t return matches[1]:

add_rewrite_rule( 'stage/shop/([^/]+)/?', "index.php?category_name=$matches[1]", 'top' );

But when I try it on pure php it return right thing:

Read More
preg_match_all('#stage/shop/([^/]+)/?#','stage/shop/audio-and-video-equipment/',$matches);
var_dump($matches[1]);

It’s my complete code:

add_action( 'init', 'wpst_init_internal' );
function wpst_init_internal()
{
    add_rewrite_rule( 'stage/shop/([^/]+)/?', "index.php?category_name=$matches[1]", 'top' );
}

add_filter( 'query_vars', 'wpst_query_vars' );
function wpst_query_vars( $query_vars )
{
    $query_vars[] = 'category_name';
    return $query_vars;
}

Is there any problem with my code?

Update:

In my query, category_name must return the matches[1] but that’s return nothing. It’s my dump:

  ["category_name"]=>
  string(0) ""

Related posts

1 comment

  1. I fix my problem:
    I enable debug mode in WordPress.
    Then I get Undefined variable: matches error, it cause of double quote, the rule should be in single quotes.

Comments are closed.