Select Page
Fixed Function _load_textdomain_just_in_time was called incorrectly

Fixed Function _load_textdomain_just_in_time was called incorrectly

You may encounter this issue in WordPress 6.7:

Function _load_textdomain_just_in_time was called incorrectly.

WordPress now warns developers if they are loading translations too early in a plugin or theme, before the current user is known. Existing functions like load_plugin_textdomain() and load_theme_textdomain() were updated to defer the actual loading to the existing just-in-time translation logic in core. This reduces the likelihood of triggering the warning, and even improves performance in some situations by avoiding loading translations if they are not needed on a given page. Here’s how you can fix this issue with straightforward solutions.

Possible Solutions

Use Correct Hooks for Loading Text Domain

If your plugin or theme is calling load_plugin_textdomain or load_theme_textdomain functions before WordPress has initialized properly, you need to call these functions on the appropriate hooks like after_setup_theme or init.

Here’s an example for each:

add_action( 'init', function () {
    load_plugin_textdomain( 'your-textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
});

For themes

add_action( 'after_setup_theme', function () {
    load_theme_textdomain( 'your-textdomain', get_template_directory() . '/languages' );
});

Adjust get_plugin_data Usage

If you’re using the get_plugin_data function to read plugin metadata, it might load translations earlier than intended. To prevent this, pass false as the third argument when calling the function:

$plugin_data = get_plugin_data( __FILE__, true, false );

Debug the Issue

If the problem persists, debugging can help pinpoint the root cause. Add the following snippet to your plugin or theme to track where _load_textdomain_just_in_time is being called:

add_action(
    'doing_it_wrong_run',
    static function ( $function_name ) {
        if ( '_load_textdomain_just_in_time' === $function_name ) {
            debug_print_backtrace();
        }
    }
);

This snippet will output a stack trace in your debug log whenever _load_textdomain_just_in_time is called incorrectly. You can use this information to identify and resolve the issue.

Need More Help?

If you’ve tried these solutions and the issue persists, feel free to reach out for assistance. Debugging text domain issues can sometimes require a closer look at your code setup

Let me know in the comments or contact me directly for further support!

Dynamically Sanitize Multi-Dimensional Array Values by Using WordPress Functions

Data sanitization is crucial in WordPress. Even this is an essential security measurement. Every data from the user’s input must be sanitized as early as possible. WordPress provides many helper functions for data sanitization you can find here. Very easy & handy function! But the real scenario may not be straightforward.

What if we need to sanitize form data having a large set of data & sanitizing one by one field may feel cumbersome? So, to get rid of this we can create a helper function that can loop through all the array elements and sanitize them one by one for us. But a little gotcha here! Of course, we may not use the same sanitization function for each field. For example: for email sanitization, we need to use sanitize_email while for the post we will use wp_kses_post.

So let’s fix the above-discussed problem:

/**
 * Sanitize array, single or multi dimensional array
 * Explicitly setup how should a value sanitize by the
 * sanitize function.
 *
 * @see available sanitize func
 * https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/
 *
 * @param array $input array to sanitize.
 * @param array $sanitize_mapping single dimensional map key value
 * pair to set up sanitization process. Key name should by inside
 * input array and the value will be callable func.
 * For ex: [key1 => sanitize_email, key2 => wp_kses_post ]
 *
 * If key not passed then default sanitize_text_field will be used.
 *
 * @return array
 */
function sanitize_array( array $input, array $sanitize_mapping = array() ):array {
	$array = array();

	if ( is_array( $input ) && count( $input ) ) {
		foreach ( $input as $key => $value ) {
			if ( is_array( $value ) ) {
				$array[ $key ] = sanitize_array( $value );
			} else {
				$key = sanitize_text_field( $key );

				// If mapping exists then use callback.
				if ( isset( $sanitize_mapping[ $key ] ) ) {
					$callback = $sanitize_mapping[ $key ];
					$value    = call_user_func( $callback, wp_unslash( $value ) );
				} else {
					$value = sanitize_text_field( wp_unslash( $value ) );
				}
				$array[ $key ] = $value;
			}
		}
	}
	return is_array( $array ) && count( $array ) ? $array : array();
}

So the sanitize_array function is a cursive function that can do sanitization each array value using respective sanitization functions.

The first argument on the function is an array that could be single or multi-dimensional. And, the second argument is that is optional but we can tell which array element should use which function. A simple key value pair, where the key is the key name from the array and the value is the respective sanitization function name that we want to use for a particular value.

Hope you enjoyed it 😀