Don’t take any action before ‘plugins_loaded’ is fired

Quite often, I see plugins calling WordPress functions before the action ‘plugins_loaded’ has been fired, i.e. before all plugins are loaded. Thus, plugins which are loaded after can’t make use of available filters.

Among the most frequent functions called as soon as the plugin is loaded are the function ‘load_plugin_textdomain’ and the function ‘get_option’. For example the code below prevents other plugins to use the filters ‘option_fantastic’, ‘override_load_texdomain’ and ‘locale’

<?php
/*
Plugin name: My fantastic plugin
*/
$my_options = get_option('fantastic');
load_plugin_textdomain('fantastic', false, basename(dirname(__FILE__)).'/languages');

That’s an issue especially with multilingual plugins which need to filter the locale before any text domain is loaded and also need to filter the options in case they contain strings which need to be translated.

Every plugin authors should prefer the code below which does exactly the same but does not conflict with other plugins:

<?php
/*
Plugin name: My fantastic plugin
*/
add_action('plugins_loaded', 'fantastic_init');
function fantastic_init {
  $GLOBALS['my_options'] = get_option('fantastic');
  load_plugin_textdomain('fantastic', false, basename(dirname(__FILE__)).'/languages');
}