translation - load_plugin_textdomain in `plugins_loaded` or `init`?

admin2025-05-31  0

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.

Share Improve this question asked Jan 1, 2019 at 21:49 T.ToduaT.Todua 5,8909 gold badges52 silver badges81 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

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/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748625787a313617.html

最新回复(0)