Determining Paths of Plugins in WordPress

WordPress plugin when you create, during creating a plugin you will often need to reference folders and files throughout the WordPress installation. Since the version of WordPress 2.6 the users can move this directory anywhere they want they have ability to this. So the good practice is that never use the hard-code or static path in the plugin. For you the WordPress has a set of functions to determine the path to the plugins directories and wp-content, as well as the directories within your plugins. By use these function in your plugins it is to verify that any paths you are referencing are correct careless `of where the actual directory might exist on the server or local machine.

Local Paths of Plugins in WordPress

For your plugin to determine the local server path, The plugin_dir_path() function you will use. Function uses for the purpose of extracts the physical location relative to the plugins directory from its file name.

<?php echo plugin_dir_path( __FILE__ ); ?>

Here in this function you can see the parameter  __FILE__ PHP constant to the plugin_dir_path(). It will return the full local server path to your plugin directory: /public_html/wp-content/plugins/imagegallery-plugin/. Let’s suppose that now you have need to reference the local path to a file in a sub directory in your plugin. This plugin_dir_path() function you can use along with the sub directory and files you want to reference, Below as shown here:

<?php echo plugin_dir_path( __FILE__ .’js/script.js’ ); ?>

The following result would produce when you run this example: /public_html/wp-content/plugins/imagegallery -plugin/js/script.js.

URL Paths of Plugins in WordPress

In your plugin directory to determine the full URL to any file, The plugins_url() function you’ll use as shown here:

<?php echo ‘<img src=”‘ .plugins_url(‘images/mylogo.jpg’, __FILE__ ). ‘”>’;?>

There are two parameters accept by this plugins_url() function you can see as well. First parameter is the path relative to the plugins URL and the second parameter is the plugin file that you want to be relative to. The above example use the __FILE__ PHP constant. So the preceding example will return a full URL to your plugin’s mylogo.jpg this file located in the images directory of your plugin,you can see as well:

<img src=” imagegallery -plugin/images/ mylogo.jpg “>

Below is a list of the many advantages of using the plugins_url() function to confine plugin file URLs:

  • It will supports the /mu-plugins plugin directory or folder.
  • It will auto detects the SSL. The returned URL would contain https://, if SSL is enabled.
  • It can detect the plugin location even if the user has moved his /wp-content directory to a custom location.
  • It will supports the Multisite of WordPress.

It has also features various functions to determine the URLs in WordPress. The functions available you can see use for the URLs:

  • home_url() — It is use for the Home URL for the current site ( base url of site)
  • includes_url() — It is use for includes directory URL ( wp-includes/)
  • content_url() — It is use for content directory URL ( wp-content/)
  • wp_upload_dir() — It is use for returns an array with location information on the configured uploads directory. For accessing the files in your plugin

For understanding the proper way to access or fetch the files in your plugins is an essential to ensure the maximum compatibility with all WordPress installations it regardless of how customized they are.