Try the following steps:
-
Add the rewrite rules for the
<language>/blog/%category%/%post_id%
structure.I used the
post_rewrite_rules
hook to add the rewrite rules, but for generating the rewrite rules, I usedWP_Rewrite::generate_rewrite_rules()
the same way WordPress core used it to generate the rewrite rules for the default permalink structure (that you set via the Permalink Settings page).add_filter( 'post_rewrite_rules', 'my_post_rewrite_rules' ); function my_post_rewrite_rules( $post_rewrite ) { global $wp_rewrite; // Generate the rewrite rules for example.com/<language>/blog/<category slug or path>/<post ID>/ $post_rewrite2 = $wp_rewrite->generate_rewrite_rules( '^zh-han[st]/blog/%category%/%post_id%', EP_PERMALINK ); // Combine the rules, with the new ones at the top. return array_merge( $post_rewrite2, $post_rewrite ); }
-
The above step ensures that the URLs do not result in a 404 error, and now, we need to filter the permalink URL generated via
get_permalink()
so that the URL uses the correct structure.Normally, one would use the
post_link
hook to replace the rewrite tag (%post_id%
in your case), but%post_id%
is a core rewrite/structure tag in WordPress, so we can simply use thepre_post_link
hook to set the structure to/blog/%category%/%post_id%/
if the post language (slug) iszh-hans
orzh-hant
. I.e. We just need to set the structure and the tag will be replaced by WordPress.Note:
pll_get_post_language()
is a Polylang function; see here for further details.add_filter( 'pre_post_link', 'my_pre_post_link', 10, 3 ); function my_pre_post_link( $permalink, $post, $leavename ) { if ( ! $leavename && is_object( $post ) && preg_match( '#^zh-han[st]$#', pll_get_post_language( $post->ID ) ) ) { $permalink = '/blog/%category%/%post_id%/'; } return $permalink; }
-
Be sure to flush the rewrite rules after you’ve added the above functions to your theme/plugin — just visit the Permalink Settings page without having to click on the Save Changes button.
Additionally, the RegEx pattern ^zh-han[st]
will match both zh-hans
and zh-hant
. You can test it here.