admin管理员组文章数量:1323723
I've a problem. I'm currently developing a plugin including a check for dependencies. So I've included a check in my plugin root class before calling all other init methods:
public function __construct() {
$this->init_core_hooks();
if ( ! Pmx_Dependencies::get_instance()->is_loadable() ) {
return;
}
$this->init_classes();
}
private function init_core_hooks(): void {
add_action( 'plugins_loaded', [ $this, 'plugins_loaded_action' ] ); // Here I'm loading my translation files
}
The problem is that in case a dependency is missing I'm adding a admin notice to WordPress but it's not getting translated:
public function __construct() {
if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( ! function_exists( 'wp_create_nonce' ) ) {
require_once ABSPATH . WPINC . '/pluggable.php';
}
if ( $this->is_yoast_installed() ) {
$admin_notice = [
'type' => 'error',
'content' => __( 'An error occured', 'pmx' )
];
$this->add_admin_notice( $admin_notice );
}
}
I found out that translation is not ready yet cause the hook where I init my translation file get's called after I'm adding the notice that something is missing...
So how should I handle this by also following the instructions for loading text files in WordPress??
Thanks for any help!
I've a problem. I'm currently developing a plugin including a check for dependencies. So I've included a check in my plugin root class before calling all other init methods:
public function __construct() {
$this->init_core_hooks();
if ( ! Pmx_Dependencies::get_instance()->is_loadable() ) {
return;
}
$this->init_classes();
}
private function init_core_hooks(): void {
add_action( 'plugins_loaded', [ $this, 'plugins_loaded_action' ] ); // Here I'm loading my translation files
}
The problem is that in case a dependency is missing I'm adding a admin notice to WordPress but it's not getting translated:
public function __construct() {
if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( ! function_exists( 'wp_create_nonce' ) ) {
require_once ABSPATH . WPINC . '/pluggable.php';
}
if ( $this->is_yoast_installed() ) {
$admin_notice = [
'type' => 'error',
'content' => __( 'An error occured', 'pmx' )
];
$this->add_admin_notice( $admin_notice );
}
}
I found out that translation is not ready yet cause the hook where I init my translation file get's called after I'm adding the notice that something is missing...
So how should I handle this by also following the instructions for loading text files in WordPress??
https://developer.wordpress/plugins/internationalization/how-to-internationalize-your-plugin/#loading-text-domain
Thanks for any help!
Share Improve this question asked Sep 14, 2020 at 19:32 Johnny97Johnny97 2147 silver badges18 bronze badges 1- You mention the code that loads your translations, but I cannot find it in your questions code. Does this check have to be in the constructor? It's generally seen as bad practice to do work in the constructor of an object. – Tom J Nowell ♦ Commented Sep 14, 2020 at 20:18
1 Answer
Reset to default 0You should move the check outside of the constructor after after_setup_theme
which is called immediately after load_textdomain
http://rachievee/the-wordpress-hooks-firing-sequence/
本文标签: phpHow can I translate something in my class constructor of my plugin in WordPress
版权声明:本文标题:php - How can I translate something in my class constructor of my plugin in WordPress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742120593a2421675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论