---
title: Add WordPress Plugins via Symlinks
date: 2012-09-06T09:39:19+00:00
modified: 2012-09-06T09:39:19+00:00
permalink: https://kaspars.net/blog/plugins-via-symlinks
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
post_tag:
  - How to
  - Plugin
  - Snippet
post_format:
  - Aside
category:
  - WordPress
---

# Add WordPress Plugins via Symlinks

WordPress doesn’t fully support adding plugins by using symbolic links in the `wp-content/plugins` folder. This is because `plugins_url()` which is often used for enqueuing plugin CSS and Javascript files uses `plugin_basename()` which in turn relies on `WP_PLUGIN_DIR` constant [for extracting the plugin’s basename](http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/link-template.php#L2050). However, the path to a symlinked plugin doesn’t include `WP_PLUGIN_DIR` and therefore it fails [at this replacement](http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/plugin.php#L567):

```
$file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
```

The solution is to use the following `plugins_url` filter:

```
// Allow symlinking this plugin
add_filter( 'plugins_url', 'your_plugin_symlink_fix', 10, 3 );

function your_plugin_symlink_fix( $url, $path, $plugin ) {
	// Do it only for this plugin
	if ( strstr( $plugin, basename(__FILE__) ) )
		return str_replace( dirname(__FILE__), '/' . basename( dirname( $plugin ) ), $url );

	return $url;
}
```

or replace all instances of `plugins_url( '/js/plugin.js', __FILE__ )` with something like:

```
WP_PLUGIN_URL . '/' . basename( __DIR__ ) . '/js/plugin.js'
```