WordPress has the plugins_url() function. According to its documentation, the function returns the absolute URL to the plugins or mu-plugin directory. The optional first parameter alters the output so that a string is appended to function’s default return value.
For example, assuming the code above is run, the return value will be similar to
http(s)://example.com/wp-content/plugins/my-plugin. The issue here is that you can type anything you want as a parameter including a fat fingered value. This introduces potential for unwanted errors. Also, hard coded values decrease your code’s portability.
Our goal will be to get the name of a plugin’s root directory dynamically without hard coding any values. This will help minimize coding errors errors and application bugs and also increase success at application portability.
How to Do It
We could use
plugin_dir_url( __DIR__ ) but the function seems to assume it is always called in a plugin’s root directory. If this were called in a sub directory of the plugin several levels deep, the result would contain a file system path that extends beyond our plugin’s root directory. This is not helpful for our exercise.
SIDE NOTE: Apparently you can use empty quotes as a parameter. For example,
plugin_dir_url('') which returns the URL for the root directory of the plugins directory. Originally, I did not know it was allowed or think of trying it to find out. But it works!
echo explode( '/', plugin_basename( __DIR__ ) );
The return result is an array of strings delimited by a forward slash (“/”). The first delimited value, or token, unquestionably identifies the name of the current plugin’s root directory. No matter where in the plugin’s directory structure the code is called this will always be true. in the example below, the plugin’s root directory name is
NOTE: If on a Microsoft Windows machine, the file system uses backslash (“\”) rather than a forward slash. Don’t worry. WordPress handles this internally by using the wp_normalize_path() function.
With our new delimited string, we use PHP’s explode() function to create an array of values to then revisit our good friend
plugins_url(). Don’t forget this function accepts as a parameter a string that is appended to the default value. In our case we want to append the first token of the array created from our previously delimited string.
|* Create an array of strings based on this file's absolute file system path as found in this plugin's root|
|* directory (not web root). Each index of the array is delimited by a forward slash "/".|
|$this_file_tokenized_path = explode( '/', plugin_basename( __DIR__ ) );|
|$this_plugin_root_dir_name = $this_file_tokenized_path[ 0 ];|
|$substr_start_pos = strpos( plugin_dir_path( __DIR__ ), $this_plugin_root_dir_name );|
|$wp_plugins_root_dir_path = substr_replace( plugin_dir_path( __DIR__ ), '', $substr_start_pos );|
|$this_plugin_root_url = plugins_url( $this_plugin_root_dir_name );|
|$this_plugin_root_dir = sprintf( '%s/%s', untrailingslashit( $wp_plugins_root_dir_path ), $this_plugin_root_dir_name );|
And there you have it! We have achieved our goal. The variables on the last two lines of the code snippet above represent URL and file system path to the root directory of the
plugins directory of a WordPress installation.
To make this code better, and even more portable, drop the code into a class that contains functions that return either the URL or directory path of the plugin root.