Skip to content

Instantly share code, notes, and snippets.

@t-hamano
Created April 18, 2025 05:12
Show Gist options
  • Save t-hamano/09103da539324f72be4ba781a04afd19 to your computer and use it in GitHub Desktop.
Save t-hamano/09103da539324f72be4ba781a04afd19 to your computer and use it in GitHub Desktop.
A WordPress MU plugin that temporarily suppresses PHP warnings that appear when translations are loaded too early
<?php
/**
* Plugin Name: Surpress Textdomain Warning
* Description: MU Plugin to suppress warnings that occur when translations are loaded too early.
* Requires at least: 6.7
* Requires PHP: 7.4
* Version: 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* This function is to suppress PHP warnings that occur when translations are loaded too early.
* Ideally, the plugin or theme itself should be fixed, but if they aren't yours and it's difficult
* to fix, this function will allow you to temporarily suppress the warning.
*/
function surpress_textdomain_warning( $trigger, $function_name, $message, $version ) {
// Return the default trigger for unrelated errors.
if ( '_load_textdomain_just_in_time' !== $function_name || '6.7.0' !== $version ) {
return $trigger;
}
// Define the textdomains that are causing the warning.
$textdomains = array(
'plugin-one',
'plugin-two',
);
// Don't trigger warning if the textdomain is in the message.
foreach ( $textdomains as $textdomain ) {
if ( strpos( $message, '<code>' . $textdomain . '</code>' ) !== false ) {
return false;
}
}
return $trigger;
}
add_filter( 'doing_it_wrong_trigger_error', 'surpress_textdomain_warning', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment