SOLUTION
I’ve gone with Update 3 and have got it working. What I had misunderstood, was that I believed that a rewrite rule would change designers/?designer=some-designer&bid=9
to /designers/some-designer
. But it is of course the other way around.
I’ve been doing some research, but I don’t quite get how to use add_rewrite_rule().
This post is pretty close to what I need, but do I really need that much code?
My current link is this:
http://www.norwegianfashion.no/designers/?designer=Batlak-og-Selvig&bid=9
What I want is this
http://www.norwegianfashion.no/designers/Batlak-og-Selvig/
- How do I use
add_rewrite_rule()
? - I need to pass the designer ID (bid). How can I do that when using re.write rules with out it showing in the URL?
Update 1
I found this which is a bit more explainatory. But it’s not working:
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array(
'('.$template_page_name.')/designers/(.*?)/?([0-9]{1,})/?$' => 'designers.php?designer=".
$wp_rewrite->preg_index(1)."&bid='.
$wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
function query_vars($public_query_vars) {
$public_query_vars[] = "designer";
$public_query_vars[] = "bid";
return $public_query_vars;
}
add_filter('query_vars', 'query_vars')
Update 2
Here is another solution I’ve tried, that Jan Fabry suggesten in another thread. I put this in my functions.php, but it has no effect.
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');
/* Custom re-write rules for desigenrs
------------------------------------------------------------------------------*/
function add_rewrite_rules( $wp_rewrite )
{
add_rewrite_rule('^designers/([^/]*)/([^/]*)$ /designers/?designer=$1&bid=$2 [L]', 'top');
flush_rewrite_rules(false);
}
function query_vars($public_query_vars) {
$public_query_vars[] = "designer";
$public_query_vars[] = "bid";
return $public_query_vars;
}
Update 3
I found a few examples here as well. But this is not working either 🙁
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');
// Remember to flush_rules() when adding rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules['(designers)/(\d*)$'] = 'index.php?pagename=designers&designer=$matches[1]';
return $newrules + $rules;
}
// Adding the bid var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
array_push($vars, 'designer');
return $vars;
}
Update 4
I also tried this solution, but still no sucess.
add_filter('search_rewrite_rules', 'designers_createRewriteRules');
function designers_createRewriteRules($rewrite) {
global $wp_rewrite;
// add rewrite tokens
$designer_token = '%designers%';
$wp_rewrite->add_rewrite_tag($designer_token, '(.+)', 'designers=");
$bid_token = "%bid%';
$wp_rewrite->add_rewrite_tag($bid_token, '(.+)', 'bid=');
$keywords_structure = $wp_rewrite->root . "designers/$designer_token/$bid_token";
$keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
return ( $rewrite + $keywords_rewrite );
}
Update 5
It seems that my rewrite rules are not added.
Executing the follwoing afte radding new rules: print_r($wp_rewrite)
, does not include my new rewrite rules.
Update 6
I tested Jan’s solution. But URL does not change 🙁
/* Initiation script for NF template
------------------------------------------------------------------------------*/
add_action('after_setup_theme','norwegianfashion_setup');
if ( ! function_exists( 'norwegianfashion_setup' ) ):
function norwegianfashion_setup() {
/* Here I do a lot of stuff */
// SEO friendly URL for desigenrs
add_action( 'init', 'wpa5413_init' );
add_filter( 'query_vars', 'wpa5413_query_vars' );
}
endif;
/* Custom re-write rules for desigenrs
------------------------------------------------------------------------------*/
function wpa5413_init()
{
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
'designers/([^/]+)/?',
'index.php?pagename=designers&designer=$matches[1]',
'top' );
}
function wpa5413_query_vars( $query_vars )
{
$query_vars[] = 'designer';
return $query_vars;
}
I would really appreciate it if someone were able to help me solve this issue.