Why it happens
The error 404 on the th/<page slug>
pages occurs because your custom rewrite rules overwrite the default Page rules which would otherwise load the correct Page (having the slug <page slug>
).
How to fix the issue
If your permalink structure is /%postname%
, i.e. example.com/<post slug>
, then a simple fix to the error 404 is by using the post_rewrite_rules
hook instead of rewrite_rules_array
.
So all you need to do is:
// Change this:
add_filter( 'rewrite_rules_array','customLangaugeSlugRules');
// to this one:
add_filter( 'post_rewrite_rules','customLangaugeSlugRules');
Then flush the rewrite rules — but as the WP_Rewrite::flush_rules()
documentation says: (*slightly reformatted)
Because this function can be extremely costly in terms of
performance, it should be used as sparingly as possible – such as
during activation or deactivation of plugins or themes. Every attempt
should be made to avoid using it in hooks that execute on each page
load, such as
init
.
So keep that in mind, and you could actually flush the rules manually by simply going to the Permalink Settings admin page (wp-admin
→ Settings → Permalink).