...

Managing WordPress Development With Symlinks

managing-wordpress-development-with-symlinks

In this article, I’ll show you a great way to share a single instance of a plugin or theme with multiple installations of WordPress – symlinks.

A symlink – or symbolic link – is a special file that is actually a reference to another file or folder. If you’re a Windows user you probably already use Desktop shortcuts which are a kind of symlink since they point to a file somewhere else on your system.

The key to us is that they can point to folders as well. Since WordPress 3.9 symlinks have been allowed for plugins and themes which allows us to store them elsewhere and hook them up to our installs.

Let’s assume that you have a localhost setup with a root directory. Each directory within is a separate WordPress installation. Normally you would install your new plugin in all of them. An error found in one would need to be changed everywhere, making the process long and error-prone.

With symlinks you can keep your plugins and themes in a separate folder and – using symlinks – you can point it into each installation you have. Each installation uses the same files which makes modifications and maintenance a breeze.

If you have multiple domains on the same server you can pull off the same trick. As long as the files for your websites occupy the same file system and your environment allows for symlinks (most do), you can use this method.

My favorite use for this method is handling themes and plugins from Github repositories. I prefer using git as my version control system and my projects have a structure which puts the actual theme/plugin in a sub-directory.

This means that I can’t use the folder as-is in the wp-content folder. Normally I would have to manually copy-paste the changes into the repo or change the repo’s structure. With symlinks I can just symlink the folder I need.

The process of creating a symlink is the same on Windows and Linux based systems (like OSX), but the syntax is a little different. When creating them you’ll need to specify the location of the symlink and the target.

In our case, the location of the symlink will be a folder in the themes or plugins directory. The target will refer to the directory that contains the actual plugin/theme files.

Let’s look at an example where we would use a symlink to link to a plugin in a separate folder. In the example, the root of our project contains multiple WordPress installations and a projects directory which contains plugins and themes.

Symlinks on Windows

Let’s assume that the root directory for our web projects is in C:/websites. You’ll need to open the command prompt and navigate to a WordPress install’s plugin folder and within that folder issue the following command:

mklink C:\websites\projects\my-plugin-github\my-plugin\trunk my-plugin

This command will create a my-plugin folder in your plugins folder which will lead to the folder at C:\websites\projects\my-plugin-github\my-plugin\trunk. When loading the plugin list and processing code WordPress will follow this symlink, making it seem like you have a regular plugin.

Symlinks on OSX and Linux

Let’s assume that the root directory for our web projects is in /Users/danielpataki/websites. You’ll need to open the terminal and navigate to a WordPress install’s plugin folder and within that folder issue the following command:

Looking for ways to improve your WordPress development workflow?

Kinsta’s hosting solution was built by developers for developers. Git, PHP 7, SSH, and WP-CLI, along with powerful staging and cloning environments gives you the tools you need to build sites faster!

ln -s /Users/danielpataki/websites/projects/my-plugin-github/my-plugin/trunk my-plugin

This command will create a my-plugin folder in your plugins folder which will lead to the folder at /Users/danielpataki/websites/projects/my-plugin-github/my-plugin/trunk. When loading the plugin list and processing code WordPress will follow this symlink, making it seem like you have a regular plugin.

Summary

You can repeat the process as needed for all your WordPress projects on the same system. At the end of the day, all your installations will use the same files so you can fix a bug once and see it disappear from all WordPress installs.

I use this method a lot in my day-to-day programming. If you do too and you have some tricks up your sleeve, or you have a better way of doing things, let us know in the comments below!

Discover more from WIREDGORILLA

Subscribe now to keep reading and get access to the full archive.

Continue reading