Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load only admin bar translation on WordPress frontend
/**
* Set en_US lang code if not in admin.
*/
function md_set_en_us_locale( $locale ) {
if ( ! is_admin() ) {
if ( ! defined( 'MD_REAL_LANG' ) )
define( 'MD_REAL_LANG', $locale );
$locale = 'en_US';
}
return $locale;
}
add_filter( 'locale', 'md_set_en_us_locale', 1 );
/**
* Set real lang code.
*/
function md_set_real_locale( $locale ) {
if ( defined( 'MD_REAL_LANG' ) )
$locale = MD_REAL_LANG;
return $locale;
}
/**
* Load translations for admin bar.
*
* This loads only deafult (ie. core WP) translations,
* it's possible to load plugins & themes translations
* but that requeries more code.
*/
function md_load_translations() {
unload_textdomain( 'default' );
add_filter( 'locale', 'md_set_real_locale', 2 );
load_default_textdomain();
}
add_action( 'admin_bar_init', 'md_load_translations', 1 );
add_action( 'wp_before_admin_bar_render', 'md_load_translations', 1 );
/**
* Unload translations after admin bar
*/
function md_unload_translations() {
unload_textdomain( 'default' );
remove_filter( 'locale', 'md_set_real_locale', 2 );
load_default_textdomain();
}
add_action( 'wp_after_admin_bar_render', 'md_unload_translations', 1000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.