Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Created December 29, 2017 21:16
Show Gist options
  • Save vielhuber/758904958aa559f62162df2e497fb856 to your computer and use it in GitHub Desktop.
Save vielhuber/758904958aa559f62162df2e497fb856 to your computer and use it in GitHub Desktop.
#wordpress polylang disable language in frontend
<?php
if( !is_admin() && $pagenow != 'wp-login.php' && pll_current_language() == 'en' )
{
wp_redirect(site_url().'/de/');
die();
}
@GwynethLlewelyn
Copy link

You, sir, are a genius. I wonder why Polylang charges €€€ for that simple line of code...

I did my own variant, to redirect potentially existing URLs in an 'unwanted' language (also English, in my case) to the 'main' language of the site — by removing the '/en/' bit and replacing it with a '/':

if( !is_admin() && $pagenow != 'wp-login.php' && pll_current_language() == 'en' )
{
	$current_slug = add_query_arg( array(), $wp->request );
	if( strpos("/en/", $current_slug ) !== FALSE )
	{
		$redirect_slug = str_replace("/en/", "/", $current_slug);
		wp_redirect( home_url( $redirect_slug ) );
	} else {
		wp_redirect( site_url() );
	}
	die();
}

But of course, this doesn't work when the slugs are already translated, and, worse than that, it enters a redirection loop...

@vielhuber
Copy link
Author

Hey, where can I apply these changes?

In functions.php of your active theme.

@Naudrius1
Copy link

Hey, where can I apply these changes?

In functions.php of your active theme.

For some reason is not working on my website any chance it could be because I am using child theme?

@vielhuber
Copy link
Author

Try to debug what exactly is not working. Does the first/second if clause work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment