Using WordPress ‘load_plugin_textdomain()’ PHP function

The load_plugin_textdomain() WordPress PHP function loads a plugin’s translated strings.

Usage

load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false );

Parameters

  • $domain (string) – Required. Unique identifier for retrieving translated strings.
  • $deprecated (string|false) – Optional. Deprecated. Use the $plugin_rel_path parameter instead. Default: false.
  • $plugin_rel_path (string|false) – Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides. Default: false.

More information

See WordPress Developer Resources: load_plugin_textdomain()

Examples

Basic usage

Load plugin textdomain during the init action.

add_action( 'init', 'wpdocs_load_textdomain' );

function wpdocs_load_textdomain() {
  load_plugin_textdomain( 'wpdocs_textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}

Load textdomain during after_setup_theme

add_action( 'after_setup_theme', 'wpdocs_load_textdomain' );

function wpdocs_load_textdomain() {
  load_plugin_textdomain( 'wpdocs_textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}

Load textdomain during plugins_loaded

add_action( 'plugins_loaded', 'my_plugin_init' );

function my_plugin_init() {
  load_plugin_textdomain( 'my-textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}

Specify a custom path

Load the plugin textdomain with a custom relative path to the .mo file.

add_action( 'init', 'wpdocs_load_textdomain' );

function wpdocs_load_textdomain() {
  load_plugin_textdomain( 'wpdocs_textdomain', false, 'custom-path/languages' );
}

Use a constant for the textdomain

define( 'MY_PLUGIN_TEXTDOMAIN', 'my-textdomain' );

add_action( 'init', 'my_plugin_load_textdomain' );

function my_plugin_load_textdomain() {
  load_plugin_textdomain( MY_PLUGIN_TEXTDOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}