Skip to content

Instantly share code, notes, and snippets.

@deckerweb
Created March 14, 2018 10:42
Show Gist options
  • Save deckerweb/07413db97c0bb4b0c3ada52fe4384a76 to your computer and use it in GitHub Desktop.
Save deckerweb/07413db97c0bb4b0c3ada52fe4384a76 to your computer and use it in GitHub Desktop.
Polylang plugin - switch to English locale -- below Code Snippet should go into "Code Snippets" plugin at best (please avoid functions.php or similar hacks)!
<?php
/** Do NOT include the opening php tag */
add_action( 'admin_init', 'ddw_polylang_switch_to_english_locale', 100 );
/**
* Load default English (en_US) locale when using Polylang (and, for example,
* with Elementor Page Builder) and are in an Non-English context, like
* editing a page in Swedisch, German, or whatever.
* NOTE: Change the instances of 'en_US' to the locale you want.
*
* @author David Decker - DECKERWEB
* @link https://gist.github.com/deckerweb/07413db97c0bb4b0c3ada52fe4384a76
*
* @global string $GLOBALS[ 'language' ]
*/
function ddw_polylang_switch_to_english_locale() {
/**
* Check if "Polylang" plugin is installed and if we are in Non-English context.
*/
if ( defined( 'POLYLANG_VERSION' ) && ( 'en_US' !== $GLOBALS[ 'language' ] ) ) {
/** If both yes, then switch back to English */
switch_to_locale( 'en_US' );
} // end if
} // end function
@deckerweb
Copy link
Author

What it does: checks if Polylang is in use, AND if you are in Non-English context, if both yes, switch back to default English locale (which is 'en_US'). You can change the locale in the snippet maybe to 'se_SE' or whatever you want. I tested with Polylang and Elementor and it worked fine: I could edit a German page in Elementor and still have the full English wording of Elementor (instead of German translations).

The snippet code gets only loaded within the Admin! Since Elementor's Editor is loaded technically within the Admin that is fine - frontend is not touched in any way.

Snippet should go into "Code Snippets" plugin!

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