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 have already followed the tutorial here: http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ But it still doesn't work.

In the function.php, my code is

    //decode wordpress url to jquery string
    function add_query_vars($aVars) {
    $aVars[] = "lastName"; 
    return $aVars;
    }

    // hook add_query_vars function into query_vars
    add_filter('query_vars', 'add_query_vars');

    function add_rewrite_rules($aRules) {
    $aNewRules = array('^physician-profile/([^/]*)/?' => 'physician-profile?lastName=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
    }
    // hook add_rewrite_rules function into rewrite_rules_array
    add_filter('rewrite_rules_array', 'add_rewrite_rules');

I would like my link from

    physician-profile?lastName=Rae-Lee 

to

    physician-profile/Rae-Lee.

In the pysician-profile page, my code is: but I don't know why I put it here. It seems like just return the value of lastName.

     if(isset($wp_query->query_vars['lastName'])) {
     $lastNameS=(urldecode($wp_query->query_vars['lastName'])); 

In the main page,I linked the physician link as

    physician-profile/Rae-Lee, 

but it keep going to the home page.When I tried changed the link as

    physician-profile?lastName=Rae-Lee, 

it goes directly that page, but the URL doesn't change. As a wordpress beginner, could you point it out which part I did wrong? Or do I need other codes? Thank you for your help!

share|improve this question
    
It seems, you did not call/hook the add_rewrite_rules function...? Or did I just overlook that? –  t f Mar 15 '13 at 15:50
    
Oh, I forgot to paste and copy them. Already added them in the code. –  Pluto Mar 15 '13 at 15:52
add comment

1 Answer

up vote 0 down vote accepted

The tutorial is outdated and it seems it missed to flush the rules.

function add_rewrite_rule_and_tag() {

    global $wp_rewrite;

    add_rewrite_rule( '^physician-profile/([^/]*)/?', 'physician-profile?lastName=$matches[1]', 'top' );
    add_rewrite_tag( '%lastName%','([^&]+)' );

    if ( ! isset( $wp_rewrite->rules['^physician-profile/([^/]*)/?'] ) )
        $wp_rewrite->flush_rules();

    return;

}

add_action( 'init', 'add_rewrite_rule_and_tag', 99 );

You can access the query var `lastName´ with

gloabl $wp_query;
$lastName = $wp_query->query_vars['lastName'];
share|improve this answer
    
Thank you Ralf, but I tried to put codes in the function.php, and then nothing happened. I even used rewrite analyzer but it the new rule didn't show in the analyzer. –  Pluto Mar 18 '13 at 14:18
    
I figured it out. Thank the link should be index.php?pagename=physician-profile..balabala –  Pluto Mar 18 '13 at 18:38
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.