The WordPress documentation for Determining Plugin and Content Directories states that:
WordPress makes use of the following constants when determining the
path to the content and plugin directories. These should not be used
directly by plugins or themes, but are listed here for completeness.
It goes on to list WP_CONTENT_DIR
and WP_PLUGIN_DIR
among constants that theme and plugin developers should not use, presumably because of this:
WordPress allows users to place their wp-content directory anywhere
they want, so you must never assume that plugins will be in
wp-content/plugins, or that uploads will be in wp-content/uploads, or
that themes will be in wp-content/themes.
Mark Jaquith also comments here that those constants should not be used:
Don’t use WP_PLUGIN_URL or WP_PLUGIN_DIR — plugins might not be in the
plugins directory.
So, what is the accepted way of referencing the full path to the plugins, wp-content, and themes folders without using these constants?
As a simple example, to output the full path of all installed plugins, I can do this:
<?php
$plugins = get_plugins();
foreach ($plugins as $file => $details) {
echo WP_PLUGIN_DIR . "https://wordpress.stackexchange.com/" . $file . '<br>';
}
Which produces a list like so:
/var/www/wp-content/plugins/akismet/akismet.php
/var/www/wp-content/plugins/debug-bar/debug-bar.php
/var/www/wp-content/plugins/hello.php
(I might want to do this if I was writing a plugin to allow the user to selectively archive plugins as part of a site backup, for example.)
If using WP_PLUGIN_DIR
is wrong, what is the suggested alternative? There is no equivalent to wp_upload_dir()
for the plugins, themes, and wp-content folder that I can find, which makes referencing the potentially wandering themes and plugins root directories problematic.