WordPress URL Rewrite not working

I’m trying to create a custom url rewrite for my wordpress plugin.

function insert_plugin_rewrite_rule($rules) {
    global $wp, $wp_rewrite;
    $ret = $wp_rewrite->add_rule('updates/plugins/([^/]+)/', 'index.php?update_slug=$matches[1]', 'top');

    // Remove when debugging is done.

add_filter('init', 'insert_plugin_rewrite_rule');

And to try and intercept it:

function blah() {
    global $wp;
    echo "<pre>";print_r($GLOBALS['wp']->query_vars['update_slug']);echo "</pre>";
add_filter('init', 'blah');

When I’m in the ‘blah’ function, it shows that my update_slug query var has been registered, and that my url has been registered too. But when I go to /updates/plugins/123/ it doesn’t show any query_vars have been set.

Am I doing this right, or is there some subtle thing I’m missing?

Solutions Collecting From Web of "WordPress URL Rewrite not working"

first, you should use the proper filter and method to add query vars and rewrite rules and not manipulate the globals directly. the other issue I believe is your regex pattern, this is working for me:

add_filter( 'query_vars', 'wpa59404_query_vars' );
function wpa59404_query_vars($query_vars){
    $query_vars[] = 'update_slug';
    return $query_vars;

add_action( 'init', 'wpa59404_rewrites' );
function wpa59404_rewrites(){