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
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
You need to create a function to add query variables to query string and then hook that function into the query_vars hook:
then we need to add the rewrite rule:
function add_rewrite_rules($aRules) {
Getting the variable:
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:
Then hook generate rules like this:
add the variable to query vars:
bonus:
To debug your rules you can use this function:
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!!!