wordpress passing parameter to custom template

I am using wordpress.
I have created a page dealer-profile from admin and assigned a template to it. Now I want to pass a parameter like

SITE-URL/dealer-profile/SUV

I have added following into my .htaccess

Read More
RewriteRule ^dealer-profile/([a-zA-Z0-9_-]+)(|/)$ index.php?pagename=dealer-profile&dealer=$1 [QSA]

I have also tried following

add_rewrite_rule('dealer-profile/([^/]+)', 'index.php?pagename=dealer- profile&dealer=$matches[1]', 'top');
flush_rewrite_rules(false);

when I am requesting SITE-URL/dealer-profile/SUV, it automatically redirects to SITE-URL/dealer-profile

Please suggest, where I am wrong.

Now I am doing following

function themeslug_query_vars( $qvars ) {
  $qvars[] = 'dealer';
  return $qvars;
}
add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );
function add_rewrite_rules($aRules) {
    $aNewRules = array('dealer-profile/([^/]+)/?$' => 'index.php?pagename=dealer-profile&dealer=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');

But this is working for digits only like
– test/dealer-profile/234234/ is working
– test/dealer-profile/234234/ is not working

Solution

Ref : http://codex.wordpress.org/Class_Reference/WP_Rewrite

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['(dealer-profile)/(.*)$'] ) ) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}

// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
    $newrules = array();
    $newrules['dealer-profile/(.*)$'] = 'index.php?pagename=dealer-profile&dealer=$matches[1]';
    return $newrules + $rules;
}

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

Thanks A lot for Your Help @@jnhghy – Jantea Alexandri

Related posts

Leave a Reply

1 comment

  1. You need to create a function to add query variables to query string and then hook that function into the query_vars hook:

    function add_query_vars($aVars) {
    $aVars[] = "dealer-profile"; // represents the name of the variable that you want to pass
    }
    
    // hook add_query_vars function into query_vars
    add_filter('query_vars', 'add_query_vars');
    

    then we need to add the rewrite rule:
    function add_rewrite_rules($aRules) {

    function add_rewrite_rules($aRules) {
    $aNewRules = array('dealer-profile/([^/]+)/?$' => 'index.php?pagename=dealer-profile=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
    }
    
    // hook add_rewrite_rules function into rewrite_rules_array
    add_filter('rewrite_rules_array', 'add_rewrite_rules');
    

    Getting the variable:

    if(isset($wp_query->query_vars['dealer-profile'])) {
    $sMsdsCat = urldecode($wp_query->query_vars['dealer-profile']);
    }
    

    and this should do the trick,
    and here are some extra link for “advanced rules”:
    http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/
    http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html

    EDIT: let us try the steps that we have in the second article:
    First let’s create the tag and add it to the rewrite rules (this should work for alpha-numeric values:

    function createRewriteRules() {
        global $wp_rewrite;
    
        // add rewrite tokens
        $dealertag = '%dealertag%';
        $wp_rewrite->add_rewrite_tag($dealertag, '(.+?)', 'dealer-profile=');
    
        $keywords_structure = $wp_rewrite->root . "dealer-profile/$dealertag/";
        $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
    
        $wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules;
        return $wp_rewrite->rules;
    }
    add_action('generate_rewrite_rules', 'createRewriteRules');
    

    Then hook generate rules like this:

    function add_rewrite_rules( $wp_rewrite ) 
    {
            add_rewrite_rule('^dealer-profile/([^/]*)/?','index.php?page_id=12&dealer-profile=$matches[1]','top');
    }
    add_action('generate_rewrite_rules', 'add_rewrite_rules');
    

    add the variable to query vars:

    function query_vars($public_query_vars) {
        $public_query_vars[] = "dealer-profile";
        return $public_query_vars;
    add_filter('query_vars', 'query_vars');
    

    bonus:
    To debug your rules you can use this function:

    function dev4press_debug_rewrite_rules() {
      global $wp_rewrite;
      echo '<div>';
      if (!empty($wp_rewrite->rules)) {
        echo '<h5>Rewrite Rules</h5>';
        echo '<table><thead><tr>';
        echo '<td>Rule</td><td>Rewrite</td>';
        echo '</tr></thead><tbody>';
        foreach ($wp_rewrite->rules as $name => $value) {
          echo '<tr><td>'.$name.'</td><td>'.$value.'</td></tr>';
        }
        echo '</tbody></table>';
      } else {
        echo 'No rules defined.';
      }
      echo '</div>';
    }
    

    it’s from https://docs.dev4press.com/tutorial/wordpress/debug-wordpress-rewrite-rules-matching/ and it should show you all your rules in a table format, just include it in any of your templates and check if the above functions are adding the rewrite rules correctly if not, it should give you some extra info about what didn’t worked.

    And you can also use the well known Developer plugin to inspect your rules, you’ll get extra info with this plugin!!!