Passing the JQuery string to Worldpress URL

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

Read More
    //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!

Related posts

Leave a Reply

1 comment

  1. 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'];