WordPress.org

Make WordPress Core

Opened 13 months ago

Last modified 11 months ago

#23794 reopened enhancement

load_plugin_textdomain fails if plugin is loaded from mu-plugins

Reported by: ideag Owned by:
Milestone: Future Release Priority: normal
Severity: minor Version: 3.5.1
Component: I18N Keywords:
Focuses: Cc:

Description

load_plugin_textdomain is realative to WP_PLUGIN_DIR, so it fails to load localization files if plugin keeps them in its directory.

The function should check if is called from mu-plugins and use WP_MUPLUGIN_DIR in that case.

I can fix this in my own plugin by adding '../mu-lugins/' like this:

  load_plugin_textdomain( 'tiny_tribuna', false, '../mu-plugins/'.dirname( plugin_basename( __FILE__ ) ) . '/languages/');


but this is not a good option for other plugins.

Attachments (1)

23794.patch (1.6 KB) - added by johnbillion 12 months ago.

Download all attachments as: .zip

Change History (10)

comment:1 ocean9013 months ago

  • Keywords close added

You should use load_muplugin_textdomain() then.

comment:2 ideag13 months ago

  • Resolution set to fixed
  • Status changed from new to closed

Problem is, a lot of plugins only use load_plugin_textdomain() :| That means I would have to modify them after every upgrade. Oh well, I guess I can contact the creators of those plugins,

comment:3 ocean9013 months ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution changed from fixed to worksforme

comment:4 SergeyBiryukov13 months ago

Here's a workaround I wrote for one of my plugins in case someone decided to put it in mu-plugins:

if ( false !== strpos( __FILE__, basename( WPMU_PLUGIN_DIR ) ) )
	load_muplugin_textdomain( '...' );
else
	load_plugin_textdomain( '...', false, dirname( plugin_basename( __FILE__ ) ) );

comment:5 follow-up: nacin13 months ago

  • Milestone set to Future Release
  • Resolution worksforme deleted
  • Status changed from closed to reopened
  • Type changed from defect (bug) to enhancement

We should probably merge these functions to look in both directories — plugins then mu-plugins.

comment:6 in reply to: ↑ 5 ideag13 months ago

Replying to nacin:

We should probably merge these functions to look in both directories — plugins then mu-plugins.

something like this?

function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) {
          $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
	
	        if ( false !== $plugin_rel_path ) {
	                $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
	        } else if ( false !== $abs_rel_path ) {
	                _deprecated_argument( __FUNCTION__, '2.7' );
	                $path = ABSPATH . trim( $abs_rel_path, '/' );
	        } else {
	                $path = WP_PLUGIN_DIR;
	        }

                if (!is_readable($path)) {
                        $path = WPMU_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
                }
	
	        $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
	        return load_textdomain( $domain, $mofile );
}

johnbillion12 months ago

comment:7 johnbillion12 months ago

23794.patch adds the mu-plugin fallback from ideag and updates the inline docs and deprecated argument so it's in line with other deprecated arguments.

comment:8 TroyDesign12 months ago

I solved using code below:

add_filter( 'load_textdomain_mofile', 'tdc_mu_load_textdomain_mofile', 10, 2 );
function tdc_mu_load_textdomain_mofile($mofile, $domain) {
	$mumofile = WPMU_PLUGIN_DIR.preg_replace('%^'.WP_PLUGIN_DIR.'%','',$mofile);
	if (file_exists($mumofile)) return $mumofile;
	else return $mofile;
}

or

add_filter( 'load_textdomain_mofile', 'tdc_mu_load_textdomain_mofile', 10, 2 );
function tdc_mu_load_textdomain_mofile($mofile, $domain) {
	if (preg_match('%^'.WP_PLUGIN_DIR.'(.+)%',$mofile,$m) && file_exists(WPMU_PLUGIN_DIR.$m[1])) return WPMU_PLUGIN_DIR.$m[1];
	else return $mofile;
}
Last edited 12 months ago by TroyDesign (previous) (diff)

comment:9 janw.oostendorp11 months ago

  • Cc janw.oostendorp@… added
Note: See TracTickets for help on using tickets.