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.

Basically I want to rewrite: /edit/test-post to an existing page with a parameter: /edit?e=test-post

From the examples in the Codex I created:

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules(){
    $rules = get_option( 'rewrite_rules' );

    if ( ! isset( $rules['(edit)/(\d*)$'] ) ) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}

// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
    $newrules = array();
    $newrules['(edit)/(\d*)$'] = 'edit?&e=$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
    array_push($vars, 'e');
    return $vars;
}

However, I simply cannot get it working, what am I doing wrong?

Thanks!

share|improve this question
    
Do you want the e= parameter value to be a string or digits? In your question intro it's a string/slug, but in the code your pattern (edit)/(\d*)$ would match 'edit' and then digits. –  Chris Carson Aug 6 '11 at 22:02
    
It's a slug actually. However, that didn't fix it. :( –  FLX Aug 6 '11 at 23:19
    
I've had problems with rewrite_rules myself. In my case I didn't even mess with the rules -- I just defined a custom post type with a slug that was different from the post type name. The new slug didn't work until I deleted the rewrite_rules option in the database manually (note -- not on a production site.) This leads me to believe that the WP code is buggy. Let me know if you figure it out. –  Chris Carson Aug 6 '11 at 23:48
add comment

1 Answer

up vote 1 down vote accepted

By 'existing page' do you mean a WordPress page? My experience with rewrites is routing everything to index.php with whatever extra query vars you need, I think attempting to route it to edit is one of your issues, not entirely clear on your intent there.

Go download Monkeyman Rewrite Analyzer if you don't already have it, it's a great tool for testing these things out.

I tested out the following rule to verify it works. All requests to /edit/* load a page named 'edit', get_query_var('e') returns the correct value for e in my template.

Note I didn't flush the rules here, I usually just visit the Settings > Permalinks page and click save to flush whenever I change them.

<?php

add_action( 'init', 'my_rewrites_init' );
function my_rewrites_init(){
    add_rewrite_rule(
        'edit/([^/]+)/?',
        'index.php?pagename=edit&e=$matches[1]',
        'top' );
}

add_filter( 'query_vars', 'my_query_vars' );
function my_query_vars( $query_vars ){
    $query_vars[] = 'e';
    return $query_vars;
}
share|improve this answer
    
Awesome, that worked! Thanks for the solution and Monkeyman Rewrite Analyzer, I'll be playing with it :) –  FLX Aug 7 '11 at 12:14
add comment

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.