$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>translation - load_plugin_textdomain in `plugins_loaded` or `init`?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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/

Post a comment

comment list (0)

  1. No comments so far