Which is the best/recommended way to hook load_plugin_textdomain
within - plugins_loaded
or init
? and what are drawbacks using either.
Which is the best/recommended way to hook load_plugin_textdomain
within - plugins_loaded
or init
? and what are drawbacks using either.
Load translation files as late as possible for your plugin's use-case. This allows other plugins as much time as possible to fully initialise.
Why should you care about other plugins? Because they may be involved in the localisation process too. For example, changing the site language or filtering translation file paths. They can't do those things if you beat them to it.
From your two examples: plugins_loaded
fires first, so init
is the better of the two in most cases. However, it still runs the risk that your init code fires before another plugin's init code, so set a low priority in your add_action call. (larger number = lower priority).
If your translations are needed earlier then you'll have to load them earlier. However, if you need them earlier than init then your whole setup might be firing too early.
As of WordPress 6.7, init
is the correct hook you should use. Using plugins_loaded
will now trigger a PHP warning notice like so: "Notice: Function _load_textdomain_just_in_time was called incorrectly."
See i18n improvements: https://make.wordpress/core/2024/10/21/i18n-improvements-6-7/