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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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