WordPress.org

Support

Support » Plugins and Hacks » Hacks » Custom rewrite rules aren't working

Custom rewrite rules aren't working

  • I had some custom code (which didn’t work) then cycled through the Codex examples (which didn’t work) and now I’m wondering what I’m doing wrong. The code I originally used worked properly on a different site, but it wasn’t translating. In any case, here’s what I have now.

    add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
    add_filter('query_vars','wp_insertMyRewriteQueryVars');
    add_filter('init','flushRules');
    
    // Remember to flush_rules() when adding rules
    function flushRules(){
    	global $wp_rewrite;
       	$wp_rewrite->flush_rules();
    }
    
    // Adding a new rule
    function wp_insertMyRewriteRules($rules)
    {
    	$newrules = array();
    	$newrules['(project)/(\d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]';
    	return $newrules + $rules;
    }
    
    // Adding the id var so that WP recognizes it
    function wp_insertMyRewriteQueryVars($vars)
    {
        array_push($vars, 'id');
        return $vars;
    }

    There is no “id” elemt in the wp_query array. I’ve reset my permalinks, logged in, logged out, refreshed, cleared cache, etc etc. Something’s not right. So…Where am I going wrong?

  • The topic ‘Custom rewrite rules aren't working’ is closed to new replies.