Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Jul 23 '12 at 20:32

This question came from our site for professional and enthusiast programmers.

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

1 Answer 1

up vote 3 down vote accepted

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'
    );
}
share|improve this answer
    
hey Milo will this work in a functions file of a theme also? –  pythonian29033 Oct 22 '13 at 8:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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