8

I am curious as there are three function for loading files in plugins:

plugin_dir_path() and plugins_url(), along with plugin_dir_url() So as a plugin developer, which would you use for loading php files? the main example shows: plugin_dir_path() - but if one were to write an auto loader function, which function would you use?

2 Answers 2

10

Return values of the three mentioned functions

1. plugin_dir_path( __FILE__ ) returns the servers filesystem directory path pointing to the current file, i.e. something along the lines of
/home/www/your_site/wp-content/plugins/your-plugin/includes/

This can be used for loading PHP files.

2. plugins_url() returns the web address of the current WordPress installation's plugin folder, i.e. something along the lines of
http://example.com/wp-content/plugins

3. plugin_dir_url() behaves in a very similar fashion to plugins_url(). It also returns a web address, but with a trailing slash, i.e. something along the lines of
http://example.com/wp-content/plugins/

The latter two are useful to load images, stylesheets, JS and the like.

Use-cases

Use-cases as from the main plugin file:

register_activation_hook( __FILE__, 'plugin_prefix_install' );

require_once( plugin_dir_path( __FILE__ ) . '/includes/class-plugin-sometask.php' );

wp_register_script(
    'your-script-handle',
    plugin_dir_url( __FILE__ ) . 'js/your-script.js',
    false,
    '1.0',
    true
);

load_plugin_textdomain(
    'your-text-domain',
    false,
    basename( dirname( __FILE__ ) ) . '/languages/'
);
8
  • Please don't pollute the global namespace with new constants! Slow, ugly and not collision-safe – that's anything but elegant.
    – fuxia
    Commented Apr 6, 2013 at 20:33
  • If you say so @toscho, I am willing to believe. And reconsider my approach. Leave this or remove? Commented Apr 6, 2013 at 20:35
  • why with the plugin_dir_path(FILE) does it got to /includes/ ? @JohannesPille
    – TheWebs
    Commented Apr 6, 2013 at 20:38
  • @TheWebs That will point to the absolute filesystem path of the current file it is used in - hence, whether it will give you a subfolder depends on the loacation of the file it was called from. Commented Apr 6, 2013 at 20:40
  • Your examples are good and helpful. Add the functions where their return value is actually used, and it is fine.
    – fuxia
    Commented Apr 6, 2013 at 20:41
3

plugin_dir_path() is the only one of the three that gives you the local filesystem path, ie: /var/www/site.com/wp-content/plugins/your-plugin/, and would be used for loading php files.

The other two are for retrieving public-facing URLs for loading assets like images, CSS, and JS files, ie: http://site.com/wp-content/plugins/your-plugin/.

This answer explains the difference between plugins_url() and plugin_dir_url().

3
  • +1, concise. Somewhat redundant, that I answered this as well, but I was halfway there when I got notified things had been done already. Commented Apr 6, 2013 at 20:33
  • Is there a reason to use plugin_dir_path() in code that resides in the plugin? Why not simply use a include like this? include 'lib/options.php' ?
    – user63350
    Commented Sep 7, 2018 at 10:10
  • Answering myself: Yes there is... - To avoid unintentionally including a file in the include path. "Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing." (php.net/manual/en/function.include.php)
    – user63350
    Commented Sep 7, 2018 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.