Send Polylang 404s to the Visitor's Language Homepage
Redirects visitors who hit a missing page in a secondary Polylang language to that language's homepage, so they are not dropped onto a 404 page.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
if ( ! is_404() || ! function_exists( 'pll_current_language' ) ) {
return;
}
$current = pll_current_language();
if ( ! $current || pll_default_language() === $current ) {
return;
}
wp_safe_redirect( pll_home_url( $current ), 302 );
exit;
} );
Notes
Uses a 302 so your 404 reports still show broken links. The default language keeps the normal 404 page.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.