Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
I used pll__ in functions.php without any problem. Are you sure you provided the required translation using pll_register_string or via the dashboard? –  RRikesh Apr 29 '13 at 6:31

1 Answer 1

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) of index.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 short even longer, what you need to do is to make sure you don't call pll_e() or pll__() 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 to init hook will suffice. Functions called in the global scope of functions.php will be executed before the init hook, but by adding an init hook you can make a function run at a later stage. Here's how:

function my_setup() {
    global $some_var;
    $some_var = pll__('A string in need of localization', 'localization_domain'); // This is assuming that this function behaves analogous to __() 

    pll_e("We'll just echo this, somewhere", 'localization_domain');
}
add_action('init', 'my_setup');
share|improve this answer
    
Nop... it doesn't work... :( Don't know what to do! –  Lightworker Feb 14 '13 at 9:25
    
What have you tried? Using other hooks than init? Also, I hope you didn't simply copy/paste my code as there was an error in it. :) –  Simon Feb 14 '13 at 10:52
1  
I had a look at Polylang and pll__($string) is just a wrapper around __($string, 'pll_string') which is built into WordPress. However, the plugin adds filters to override_load_textdomain, gettext and gettext_with_context. It also loads its own textdomain on the hooks wp, login_init, admin_init so likely the functions would be available after any of those. Also, once setup is complete it fires the pll_language_defined which might also we worth looking into. In general my best tip is having a look at the Polylang source in order to get an understanding of how it works. –  Simon Feb 14 '13 at 11:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.