[wp-trac] [WordPress Trac] #34358: plugin_dir_url( __FILE__ ) returns plugins directory when plugin symlinked to mu-plugins

WordPress Trac noreply at wordpress.org
Tue Apr 18 16:47:11 UTC 2017


#34358: plugin_dir_url( __FILE__ ) returns plugins directory when plugin symlinked
to mu-plugins
-------------------------------------------------+-------------------------
 Reporter:  scamartist26                         |       Owner:
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  Future
Component:  Plugins                              |  Release
 Severity:  normal                               |     Version:  4.3.1
 Keywords:  has-patch needs-unit-tests needs-    |  Resolution:
  refresh                                        |     Focuses:
-------------------------------------------------+-------------------------

Comment (by will_c):

 I also came across this issue recently while trying use MU-Plugins for a
 multi-tenant setup. I had a slightly different structure, but encountered
 a similar issue where static assets had the full path included in their
 url. Happy to share a longer write-up if useful, but not sure how widely
 relevant our use-case is.

 We have a symlinked wp root directory as well as a symlinked content
 directory that points to a directory outside the wp root. We have all of
 our plugins inside of `mu-plugins` and are using a `loader.php` to
 reference the main file for each plugin.

 Using multiple symlinks in our setup was causing issues when plugins were
 calling `plugins_url()` or `plugin_dir_url()` and passing in a reference
 to the main plugin file. `plugins_url()` calls `plugin_basename()` which
 uses `preg_replace` ([https://github.com/WordPress/WordPress/blob/master
 /wp-includes/plugin.php#L657]) to try and determine the relative path of
 the current plugin.

 In our case this was not matching the WPMU_PLUGIN_DIR because it was using
 the full path (including the symlinks) while the `$file` passed into the
 `plugin_basename()` was using the shorter direct link to the mu-plugins
 (without the symlinked folders). This caused `plugin_basename()` to return
 the full path to the plugin rather than the relative path.

 The solution was to define `WP_CONTENT_DIR` using `realpath()`:

 `define('WP_CONTENT_DIR', realpath(dirname(__FILE__) . '/content'));`

 rather than:

 `define('WP_CONTENT_DIR', dirname(__FILE__) . /content');`.

 If you're only defining `WPMU_PLUGIN_DIR` as a location outside the wp
 root, the same solution would apply, just use `realpath()` the same way as
 above rather than referencing a symlinked location directly.

 Everything works as expected now that we're using `realpath()`.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34358#comment:17>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list