Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active December 14, 2015 21:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glueckpress/ec9a10bdec49d5e2e9a1 to your computer and use it in GitHub Desktop.
Save glueckpress/ec9a10bdec49d5e2e9a1 to your computer and use it in GitHub Desktop.
[WordPress] Exclude text domains from language files by wrapping them into custom functions instead of default l10n functions. (http://glck.be/6651/)
<?php
/* String from your theme/plugin */
_e( 'Margaritas!', 'your-textdomain' );
__( 'Margaritas!', 'your-textdomain' );
/* Original string from WooCommerce: */
_e( 'Product', 'woocommerce' );
__( 'Product', 'woocommerce' );
<?php
/* In your own theme/plugin replace with: */
wcl10n_e( 'Product', 'woocommerce' );
wcl10n__( 'Product', 'woocommerce' );
<?php
/**
* Wrappers for default l10n functions to include in your theme or plugin.
*/
if ( ! function_exists( 'wcl10n__' ) ) {
/**
* Wrapper for __() gettext function.
* @param string $string Translatable text string
* @param string $textdomain Text domain, default: woocommerce
* @return void
*/
function wcl10n__( $string, $textdomain = 'woocommerce' ) {
return __( $string, $textdomain );
}
}
if ( ! function_exists( 'wcl10n_e' ) ) {
/**
* Wrapper for _e() gettext function.
* @param string $string Translatable text string
* @param string $textdomain Text domain, default: woocommerce
* @return void
*/
function wcl10n_e( $string, $textdomain = 'woocommerce' ) {
echo __( $string, $textdomain );
}
}
/* repeat for other l10n function, see wp-includes/l10n.php */
@swissspidy
Copy link

The _e function in WordPress echoes the string, so you can't return it. This means your second example here doesn't quite work as expected. Use echo __( $string, $textdomain ) instead.

@glueckpress
Copy link
Author

Bummer, thanks. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment