1

I'm writing a plugin and I've just added a check for a minimum PHP version. I tried to i18n the error message but it doesn't work. I translated every string in the plugin, everything works correctly except that one, so the error is always displayed in english. Here's my code:

/**
* Plugin Name: plugintest
* Description: test
* Version: 1.0
* Author: me
* Text Domain: plugintest
* Domain Path: /languages/
*/

add_action('plugins_loaded', 'plugintest_load_plugin_textdomain');

// Languages
function plugintest_load_plugin_textdomain() {
load_plugin_textdomain( 'plugintest', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
// Check for required PHP version
if (version_compare(PHP_VERSION, '5.4', '<'))
{
     $phpoldmsg = __('This plugin requires PHP 5.4 or higher. Please upgrade the version of PHP installed on your server', 'plugintest');
     $phpoldmsg .= " (".PHP_VERSION.")";
     exit($phpoldmsg);
}
else{
    include 'plugintest.php';//INCLUDE REST OF PLUGIN   
}

I guess it's because the plugin textdomain is not loaded yet when the message is displayed but I can't figure how to solve this. I read somewhere that by adding "Text Domain" and "Domain Path" in the plugin header WP is supposed to translate the strings even before the activation, but it doesn't seem to help me. Or should I use an earlier hook than "plugins_loaded"? Thank you in advance

  • Put your php check also in separate function, so that it load under control of WP hooks. So you have the chance to load the translation before the php check. – bueltge Jul 6 '16 at 13:40
  • I'm not sure I understand what you're saying here. The time at which the translation is loaded is controlled by the hook 'plugins_loaded', how would putting the check in a separate function change this? Could you elaborate or provide an example? Thanks – Lupus Jul 7 '16 at 6:41
  • I add a answer include a source example, maybe this get more clarity. – bueltge Jul 7 '16 at 7:08
0

Your check of the php version is not in a separate function. That's the point that you not have the control about the order of the flow of this functions.

The follow example source should get more clarity about the stack. The order is depend on the hooks in the plugintest_init function. You can also use the same hook and a different priority, the third parameter for add_action.

add_action( 'plugins_loaded', 'plugintest_init' );

function plugintest_init() {

    add_action( 'init', 'plugintest_load_plugin_textdomain' );
    add_action( 'admin_init', 'plugintest_check_php_version' );
}

function plugintest_load_plugin_textdomain() {

    // ...
}

function plugintest_check_php_version() {

    // ...
}

As another hint, the source is quite simple. A class, OOP code is much better for the structure and use. but in your context of the question is it maybe easier to understand.

  • Thanks for your advice, the error message gets translated now. Unfortunately it's displayed in a blank page. I guess that's because "admin_init" happens before the backend is created. I tried changing the hook to later ones, but anything after "current_screen" just allows the plugin to be activated anyway. This is so frustrating. – Lupus Jul 7 '16 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.