I’ve on functions.php some functions that echoes blocks of HTML.
Problem comes with Polylang plugin, that uses custom functions pll_e() and pll__().
Those works right on index.php on every theme, but doesn’t work right on functions.php, wich basically always returns me the default language strings, even having’em registred on admin side.
So those blocks printed on index.php using pll_ functions, work fine. But same blocks printed on functions.php, always come in English.
I’ve posted this on oficial plugin support forum. No answers yet.
Someone here have an idea of what it can be?
Thank you so much!
I have no experience with the Polylang plugin, but I think it’s safe to assume that this is has to do with load order of the Polylang plugin and your theme’s
functions.php
. When you call a function in the global scope (i.e. not from within another function) ofindex.php
or any other template file, it will be executed at a late stage in the page loading process, after WordPress has been completely initialized and even after the server has begun sending the response to the client.A function called from the global scope of the
functions.php
file, however, will be executed at a much earlier point. During a typical page load, essentially what happens is first WordPress will set itself up, then load the plugins and at last the theme. However, this process is not as linear as you might think, as some plugins might depend on functionality which might not be available until WordPress is completely initialized, the theme is loaded or the page server response has begun. WordPress solves this by using hooks, which allows a plugin or theme to execute code at certain stages during the load process.To make a long answer
shorteven longer, what you need to do is to make sure you don’t callpll_e()
orpll__()
until you are certain the Polylang plugin is ready. This can be determined by looking at its source (or perhaps its documentation), but I guess hooking onto toinit
hook will suffice. Functions called in the global scope offunctions.php
will be executed before theinit
hook, but by adding aninit
hook you can make a function run at a later stage. Here’s how: