WordPress.org

Make WordPress Core

#47584 closed enhancement (duplicate)

Wrap __ function in function_exists

Reported by: fishpig Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: I18N Keywords:
Focuses: Cc:

Description

The translation function is named in a way that all other applications seem to name their translation function. This causes a fatal PHP error when integrating platforms into WordPress (or WordPress into other platforms). The only fix for this is to apply this patch via code, which requires write permissions. This isn't great for security.

An easy fix for this (which won't affect functionality in WordPress), is to wrap the function in a call to function_exists in the core code.

<?php

if (!function_exists('__')) {
  function __( $text, $domain = 'default' ) {
    return translate( $text, $domain );
  }
}

If WordPress is running on it's own (not integrated by another platform), the function will always be defined. If WordPress is included by another code library that has defined the function then it won't be re-defined.

As this function in WordPress is just a wrapper for the WordPress translate function, the translation function defined in another application can still access WordPress translations using this function.

<?php

function __($text, $domain = 'default') {
 if (is_currently_in_wp()) {
  // Call WordPress translate functions 
  return call_user_func_array('translate', func_get_args());
 }

 // Handle other translation stuff here
}

Change History (1)

#1 @ocean90
18 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi there and welcome to WordPress Trac!

We've already gotten the same request in #41498, thus I am going to close this one as a duplicate.

Note: See TracTickets for help on using tickets.