hooks – Admin Notice is only localized when displaying the “Plugins” Backend Page
My plugin checks if a specific theme is activated. The check is hooked on init
.
If the theme is not activated, it displays a warning, hooked on admin_notices
.
This works fine.
Now I added localization support with the help of “Loco Translate” and .mo
/ .po
files.
load_plugin_textdomain
is hooked on init
.
This works too, translations are showing up.
Now it gets weird:
My “Admin Notice” gets translated as expected, but only when opening wp-admin/plugins.php
.
On any other backend-page the non-translated version appears.
My guess is it has something to do with the hook-order, but I can not figure out how to order the checks properly.
Some code, redacted for sanity:
<?php
namespace Me\MyPlugin;
class Bootstrapping
{
public static function notifyOnMissingTheme(): void {
if( ! self::ThemeIsActivated() ) {
add_action('admin_notices', [self::class, '_echoThemeExistanceWarning']);
}
}
public static function activateTranslationSupport() : void {
load_plugin_textdomain( 'management-plugin', false, dirname(plugin_basename('__FILE__')) . '/languages');
}
private static function ThemeIsActivated(): bool
{
$theme = wp_get_theme();
if (!('My themes name' == $theme->name || 'My themes name' == $theme->parent_theme) ) {
return false;
}
return true;
}
static function _echoThemeExistanceWarning(): void {
echo '<div class="notice notice-warning"><p>'. __('Notification from Plugin: Please activate the Theme for best experience with this plugin!', 'language-domain'). '</p></div>';
}
}
<?php
/*
Plugin Name: Plugin
Text Domain: language-domain
Domain Path: /languages
*/
namespace Me/MyPlugin;
add_action('init', [Bootstrapping::class, 'activateTranslationSupport']);
add_action('init', [Bootstrapping::class, 'notifyOnMissingTheme']);
Thanks alot for your help!
Leave an answer