Setting MAMP to handle symbolic linking in WordPress
December 5, 2013
I like to work on plugin and themes keeping them in a plugins
and a themes
folder which is not nested in a particular WordPress installation but is instead at the top-level of my web-development folder.
Using symbolic linking and the plugin and themes nature of state-less machines I can then work on code in one place only and test it on many WordPress installations at once.
And then I need URLs
When using WordPress built-in functions like plugins_url
, plugin_dir_url
or anything like that the resulting URLs those functions will produce will not be what I expect
http://wordpress-it.dev/wp-content/plugins/Users/Luca/Dropbox/Developer/WebDeveloper/vhosts/plugins/member-signup-for-wordpress/assets/js/public.min.js?ver=1.0.0
in place of the correct one
http://wordpress-it.dev/wp-content/plugins/member-signup-for-wordpress/assets/js/public.min.js?ver=1.0.0
Symlinks are the problems and a solution
As discussed in many posts around the web the problem lies in using symbolic links.
In the main plugin file I can make some discrimination about it and although a convenient solution already exists I'm looking for a less radical one that will allow me to use symbolic linking both in development and production code without burdening my code too much.
I can use a constant to keep track of the eventual use of symbolic linking in my main plugin file
/**
* To deal with symbolic linking in local development
* @var string
*/
$thisFile = __FILE__;
if (defined('USE_SYMLINKS') and USE_SYMLINKS) {
$thisFile = basename(dirname(__FILE__) . '\\' . basename(__FILE__));
}
/**
* Useful constants
*/
define('MEMBERSIGNUP_PLUGIN_FILE_URL' , $thisFile);
define('MEMBERSIGNUP_PLUGIN_URL', plugin_dir_url($thisFile));
define('MEMBERSIGNUP_PLUGIN_DIRPATH', plugin_dir_path(__FILE__));
define('MEMBERSIGNUP_PLUGIN_DIRNAME', dirname(__FILE__));
/**
* Require all the classes of the plugin
*/
foreach (glob(MEMBERSIGNUP_PLUGIN_DIRPATH . 'includes/class-*.php') as $filename) {
require_once $filename;
}
/*
* Register hooks that are fired when the plugin is activated or deactivated.
* When the plugin is deleted, the uninstall.php file is loaded.
*/
register_activation_hook(__FILE__, array(
'membersignup',
'activate'
));
register_deactivation_hook(__FILE__, array(
'membersignup',
'deactivate'
));
thisFile
will default to __FILE__
which is standard for plugin and themes not installed in symbolic linked folders. A check is made on the USE_SYMLINKS
constant existence and value to eventually set it to a voodoo function call that will properly set it if using symbolic linking.
Please note that functions that need a path, like register_activation_hook
are called the normal way and are given __FILE__
as an argument since those need to attach to a path and not an URL.
Set a constant in MAMP
All of the above magic is possible if the USE_SYMLINKS
constant is set on the server and being my local server set-up using MAMP I need to
- tell the server to load an external configuration file before any other
- define the constant in said configuration file
Telling MAMP to load an external configuration file is easily made browsing MAMP menu for File > Edit Template > PHP > PHP xxx php.ini
(xxx being the PHP version I use in MAMP), and going for the auto_prepend_file
line
; Automatically add files before or after any PHP document.
auto_prepend_file =
auto_append_file =
to point it to the configuration file I want to load
auto_prepend_file = "/absolute-path-to/localConf.php"
and then create said file, mine is
// file localConf.php
<?php
/**
* Tells PHP applications that symlinking is being used
* @var bool
**/
define('USE_SYMLINKS', true);
?>