5

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

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

    // Remove when debugging is done.
    $wp_rewrite->flush_rules(false);
}

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?

  • 1
    It may also we worth noting that I'm doing my init of 'blah' a few lines after my init of 'insert_plugin_rewrite_rule' in my plugin's main file. – Jack Slingerland Jul 23 '12 at 15:45
3

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(){
    add_rewrite_rule(
        'updates/plugins/([^/]+)/?$',
        'index.php?update_slug=$matches[1]',
        'top'
    );
}
  • hey Milo will this work in a functions file of a theme also? – pythonian29033 Oct 22 '13 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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