url rewriting - Problem with add_rewrite_rule

Been trying to re-map the followingtest-sale-yachtcomfortably-numb-262749test-sale-yacht?yacht_name_id=comfortabl

Been trying to re-map the following

/test-sale-yacht/comfortably-numb-262749/
/test-sale-yacht/?yacht_name_id=comfortably-numb-262749

After some testing in a regular expression tester I've put the following command in the init routine of the new plugin

add_rewrite_rule('test-sale-yacht/([^/]*)?', 'index.php?name=test-sale-yacht&yacht_name_id=$1', 'top');

This looks similar to others so i figure it should work

I reset the permalinks several times by going to settings > permalinks and hitting save still does not work.

Tried putting it in functions.php in the theme, still no soap.

According to a debug plugin there are no rules getting hits on the page.

tried /test-sale-yacht/?yacht_name_id=comfortably-numb-262749, that works
tried /?name=test-sale-yacht&yacht_name_id=comfortably-numb-262749, that works
but /test-sale-yacht/comfortably-numb-262749/, nothing.

Totally foxed on this.

Jules

Been trying to re-map the following

/test-sale-yacht/comfortably-numb-262749/
/test-sale-yacht/?yacht_name_id=comfortably-numb-262749

After some testing in a regular expression tester I've put the following command in the init routine of the new plugin

add_rewrite_rule('test-sale-yacht/([^/]*)?', 'index.php?name=test-sale-yacht&yacht_name_id=$1', 'top');

This looks similar to others so i figure it should work

I reset the permalinks several times by going to settings > permalinks and hitting save still does not work.

Tried putting it in functions.php in the theme, still no soap.

According to a debug plugin there are no rules getting hits on the page.

tried /test-sale-yacht/?yacht_name_id=comfortably-numb-262749, that works
tried /?name=test-sale-yacht&yacht_name_id=comfortably-numb-262749, that works
but /test-sale-yacht/comfortably-numb-262749/, nothing.

Totally foxed on this.

Jules

Share Improve this question edited Jul 8, 2020 at 6:01 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Jul 7, 2020 at 14:13 JulesJules 111 bronze badge 7
  • Note that if you get your rewrite rule working you'll still need to filter the permalinks on the links, WP won't auto-adjust them. Which debug plugin are you using? Have you tried monkeyman rewrite rules? – Tom J Nowell Commented Jul 7, 2020 at 14:39
  • So I have to do more than declare the the er-write rule using add_rewrite_rule, I need to do something else as well before /test-sale-yacht/comfortably-numb-262749/ will be re-written? – Jules Commented Jul 7, 2020 at 14:47
  • Thanks for the pointer to monkeyman – Jules Commented Jul 7, 2020 at 15:05
  • The rule is not showing up at all :( – Jules Commented Jul 7, 2020 at 15:19
  • Can you add more context to the code? It's literally just the call to add_rewrite_rule with no context, I can't tell how it's being called or when or on what hook – Tom J Nowell Commented Jul 7, 2020 at 15:26
 |  Show 2 more comments

2 Answers 2

Reset to default 1

You're missing the WP $matches[1] syntax for the regex that gets matched.

I think you want /? on the end too.

Easiest way I found to test was to temporarily put flush_rewrite_rules() in whilst testing, then you don't have to do anything else. I have a similar regex which sends /foo/catname/ to index.php?taxonomy=category&term=catname, here's what I used to develop on a clean test WP install - this is working fine for me.

function foobar() {
    add_rewrite_rule('^foo/([^/]+)/?', 'index.php?taxonomy=category&term=$matches[1]', 'top');
    flush_rewrite_rules();
}
add_action('init', 'foobar', 10, 0);

So you might want to try:

add_rewrite_rule('test-sale-yacht/([^/]+)/?', 'index.php?name=test-sale-yacht&yacht_name_id=$matches[1]', 'top');

Edit: As per comment by @Tom J Nowell, flush_rewrite_rules is an expensive call - my suggestion to do this only on a dev or very low traffic site.

This worked in the end, not sure why I was not getting anything showing up in the re-write rules initially but this worked.

add_rewrite_rule( 'test-sale-yacht/([^/]*)?', 
    'index.php?pagename=test-sale-yacht&yacht_name_id=$matches[1]', 'top');

with yacht_name_id added in as a filter

Jules

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742281102a4414493.html

相关推荐

  • url rewriting - Problem with add_rewrite_rule

    Been trying to re-map the followingtest-sale-yachtcomfortably-numb-262749test-sale-yacht?yacht_name_id=comfortabl

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信