While reviewing WordPress plugins, I often kick a plugin for calling file locations poorly. This usually happens when they’ve hardcoded their plugin name or (worse) wp-content into paths.
When you hardcode in paths, or assume that everyone has WordPress in the root of their domain, you cause anyone using ‘Giving WordPress it’s own directory’ (a VERY common setup) to break. In addition, WordPress allows users to change the name of wp-content, so you would break anyone who choses to do so. And when this happens, I always link them to how to figure out the constants, which leads them to the function plugins_url()
.
In many ways, plugins_url()
is a panacea, a silver bullet, because it can take this:
<?php echo '<img src="' . plugins_url( 'images/wordpress.png', __FILE__ ) . '" > ';
And magically turn it into this:
<img src="http://www.example.com/wp-content/plugins/my-plugin/images/wordpress.png">
Even better, if you put the plugin in the mu-plugins
folder, it would know to be this:
<img src="http://www.example.com/wp-content/mu-plugins/images/wordpress.png">
That makes it insanely flexible and wonderful.
This allows me to happily rename a plugin folder to my-plugins_off to force disable it and will reward me with this error:
I like that error. I like that it happens no matter what, once I’ve renamed that plugin folder and I refresh a page that uses the plugin, it’s gone. Of course, sometimes it’s not enough, but most of the time, if you know what plugin’s being a doofus, you can fix it with that.