[wp-trac] [WordPress Trac] #47584: Wrap __ function in function_exists

WordPress Trac noreply at wordpress.org
Fri Jun 21 09:30:52 UTC 2019


#47584: Wrap __ function in function_exists
-------------------------+-----------------------------
 Reporter:  fishpig      |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  I18N         |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 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
 <?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
 <?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
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47584>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list