• I’m adding a new section to a WordPress site developed by others and based on a child theme of a purchased theme.
    To do this, I added the instruction at the bottom of functions.php:

    require_once('my_functions.php');

    Inside my_functions.php there is a hook to load the text domain used by all my texts, that is, sandbox:

    if (!function_exists('mySandobxSetup')) {
    	function mySandboxSetup() {
    		if ( is_user_logged_in() ) {
    			$lang = get_user_meta(get_current_user_id(), 'locale', false); 
    			if ( !empty($lang) ) {
    				setlocale(LC_ALL, $lang);
    			}
    		}
    		$done = load_child_theme_textdomain('sandbox', get_stylesheet_directory() . '/languages');
    		log_rec("load_child_theme_textdomain", 
    			"user", get_current_user_id(), 
    			"done", $done, 
    			"TEST", __("Click here to work with your development plan", 'sandbox')
    		);
    	}
    }
    add_action('after_setup_theme', 'mySandboxSetup');

    I use a function of mine called log_rec to register a record in log.txt file.
    Note that in the child theme directory there is a folder called languages that contains it_IT.po and .mo files.

    When I load a single page out of my section, and I logged in by using credentials associated to a user who set his preferred language as it_IT (Italian), I get the following:

    ?</img>"load_child_theme_textdomain"■"user"■303■"done"■true■"TEST"■"Clicca qui per lavorare con il tuo piano di sviluppo"■
    ?</img>"load_child_theme_textdomain"■"user"■303■"done"■false■"TEST"■"Click here to work with your development plan"■
    ?</img>"load_child_theme_textdomain"■"user"■303■"done"■true■"TEST"■"Clicca qui per lavorare con il tuo piano di sviluppo"■

    It looks like:

    1. mySandoxSetup is called three times
    2. the first time the load works, the second does not, the third works again

    BUT when I look at the page, I see the texts in English and not in Italian as expected.
    I browsed the whole site code searching for other *_textdomain() function, but there are none, so I am the only one to load a text domain.

    So I have two questions:

    1. what could make that function to be called three times,
    2. why Italian text is not shown in page (where I use __("my text", , 'sandbox'), of course), even if the last call loaded the right text domain?
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dejudicibus

    (@dejudicibus)

    PS WP-CRON is disabled, so it is NOT responsible for multiple calls to after_setup_theme hook.

    Moderator bcworkz

    (@bcworkz)

    Many WP actions fire more than once per request. I’ve no idea why, but they do. In many situations it doesn’t matter if the same process is repeated multiple times. Inefficient perhaps, but no ill effect. A possible reason for one of the extra events is if the request URL has no trailing /. Without one, WP will do a 302 redirect to an URL with the trailing /.

    In cases where repetition is a problem, you can have your callback remove itself from the action call stack so it only gets called once per request.

    Is __("my text", , 'sandbox'); really the code you’re using? (other than “my text” which doesn’t matter as long as it’s a string). Try this instead:
    __("my text", 'sandbox'); ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple calls to setup hook function and problems with loading text domain’ is closed to new replies.