-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreserve-admin-language-in-admin-bar.php
76 lines (62 loc) · 2.07 KB
/
preserve-admin-language-in-admin-bar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
Plugin Name: Polylang add-on: Preserve admin language in admin bar
Plugin Description:
Author: Aucor Oy
Version: 1.0.1
Author URI: https://www.aucor.fi
*/
/**
* Checks if current view may require admin bar language switching
*
* The function is based on is_admin_bar_showing() but cannot use it directly
* as `is_embed()` inside that function causes notices in this hook.
*
* @see https://developer.wordpress.org/reference/functions/is_admin_bar_showing/
*/
function preserve_admin_language_is_needed() {
global $show_admin_bar, $pagenow;
// for all these types of requests, we never want an admin bar.
if (defined( 'XMLRPC_REQUEST' ) || defined( 'DOING_AJAX' ) || defined( 'IFRAME_REQUEST' ) || (function_exists('wp_is_json_request') && wp_is_json_request())) {
return false;
}
if (!isset($show_admin_bar)) {
if (!is_user_logged_in() || 'wp-login.php' == $pagenow) {
$show_admin_bar = false;
} else {
$show_admin_bar = _get_admin_bar_pref();
}
}
/**
* Filters whether to show the admin bar.
*
* Returning false to this hook is the recommended way to hide the admin bar.
* The user's display preference is used for logged in users.
*
* @since 3.1.0
*
* @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
*/
$show_admin_bar = apply_filters('show_admin_bar', $show_admin_bar);
return $show_admin_bar;
}
/**
* If user has picked their preferred language, keep admin bar in that locale.
*
* @param string $mofile path to the MO file
* @param string $domain text domain
*
* @return string $mofile
*/
function preserve_admin_language_in_admin_bar($mofile, $domain) {
if (preserve_admin_language_is_needed() && $domain == 'default' ) {
$user_id = get_current_user_id();
$user_language = get_user_meta($user_id, 'user_lang', true);
if ($user_language !== '') {
$user_language = get_option('WPLANG');
}
$mofile = WP_LANG_DIR . "/$user_language.mo";
}
return $mofile;
}
add_filter('load_textdomain_mofile', 'preserve_admin_language_in_admin_bar', 10, 2);