0

Ive just been reading @gmazzap s reply about wordpress matching URLs with trailing tildes.

and could do with some help please. As a coding newbie, I am not sure where to add his suggested code:

add_action('init', function() {
  add_rewrite_tag( '%postname%', '([^~/]+)', 'name=' );
});

Functions.php, /includes/rewrite.php, or somewhere else...

Also what are the implications of doing this, when wordpress updates, will these changes be lost? Am running a child theme. Sorry to ask such basic questions. Thanks for your help.

4
  • You should add this to functions.php file of your child theme. Changes wouldn't be lost on WP updates. Jul 3 '20 at 10:08
  • If you're running a child theme, then it's generally safe to add code in your theme's functions.php file. So just add the code there in that file.
    – Sally CJ
    Jul 3 '20 at 10:12
  • I'm not sure if it applies to add_rewrite_tag, but after add_rewrite_rule() you must call flush_rewrite_rules() once, and ideally once only as it can be very slow. So if you call add_rewrite_tag and it doesn't seem to work this could be why. There's more good info here: carlalexander.ca/wordpress-adventurous-rewrite-api
    – mozboz
    Jul 3 '20 at 12:05
  • Thanks everyone for your responses so far. So I added the code to functions.php of my child theme and then did save in the permalinks area of the wp dashboard. I also cleared the superccache. At the moment, the site is still serving up pages with trailing~ . How long should I expect it to take to flush?
    – minnow
    Jul 3 '20 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.