0

I have the following pairs:

add_rewrite_rule('^questions/ask/?','index.php?pagename=ask','top');
if(is_page('ask')){
        $tname = 'add-question.php';
        if(file_exists(TEMPLATEPATH.'/'.$tname)){
            $returntemplate = TEMPLATEPATH.'/'.$tname;
        }else{
            $returntemplate = $plugindir.'/default-template/'.$tname;
        }
        ahaali_do_theme_redirect($returntemplate);

and:

add_rewrite_rule('^questions/ed/([^/]*)/?','index.php?pagename=ed&id=$matches[1]','top');
if(is_page('ed')){
            $tname = 'edit-question.php';
            if(file_exists(TEMPLATEPATH.'/'.$tname)){
                $returntemplate = TEMPLATEPATH.'/'.$tname;
            }else{
                $returntemplate = $plugindir.'/default-template/'.$tname;
            }
            ahaali_do_theme_redirect($returntemplate);
        }

Now what happens is the following: The first rule and template redirect works while the second one does not. I have added in the header of my theme

print_r($wp->query_vars); 

the result is:

Array ( [pagename] => ed [id] => 10 ) 

So the rule itself works but the template redirect does not. Any idea why?

1

From what I recall, if a page is a child of another page, the pagename value is the full parent / child path. In your examples it would be pagename=questions/ask and pagename=questions/ed, though you say the first rule works, which is puzzling.

also, side note - there's a locate_template function that will correctly handle parent / child themes.

|improve this answer|||||
0

maybe you need add your own query_var to get work

// Add query_var
function add_question_var($question_vars) {
    $question_vars[] = 'ed';    
    return $question_vars;
}
//add a rewrite rule
function do_rewrite_question() {
    global $wp_rewrite;
    add_rewrite_rule('question/ask/?$', 'index.php?pagename=ask','top');
    add_rewrite_rule('question/ed/(.+)/?$', 'index.php?pagename=question&ed=$matches[1]','top');    
}
function template_redirect_file()
{
    global $wp_query;
    if ( get_query_var('pagename')=='ask')
    {
    // Do your stuff
    }
    if ( $wp_query->get('ed'))
    {
    // Do your stuff
    }   

}
// redirect_canonical
function noredir($redirect_url)
{
    $vars = array (
    'ed' => get_query_var('ed'),    
    );
    if (!empty($vars))
    return false;
}


add_filter('query_vars', 'add_question_var');
add_action('init', 'do_rewrite_question');
add_action('template_redirect', 'template_redirect_file');
add_filter('redirect_canonical', 'noredir');

then you should update the permalink via wp-admin/options-permalink.php to test the rewrite work, add this code in add-question.php

<?php
if(isset($wp_query->query_vars['pagename'])) {
    echo urldecode($wp_query->query_vars['pagename']);
}
?>

and in edit-question.php

<?php
if(isset($wp_query->query_vars['ed'])) {
    echo urldecode($wp_query->query_vars['ed']);
}
?>
|improve this answer|||||

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.