Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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/'
);
share|improve this answer
    
Please don't pollute the global namespace with new constants! Slow, ugly and not collision-safe – that's anything but elegant. –  toscho Apr 6 '13 at 20:33
    
If you say so @toscho, I am willing to believe. And reconsider my approach. Leave this or remove? –  Johannes Pille Apr 6 '13 at 20:35
    
why with the plugin_dir_path(FILE) does it got to /includes/ ? @JohannesPille –  TheWebs Apr 6 '13 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. –  Johannes Pille Apr 6 '13 at 20:40
    
Your examples are good and helpful. Add the functions where their return value is actually used, and it is fine. –  toscho Apr 6 '13 at 20:41
show 2 more comments

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().

share|improve this answer
    
+1, concise. Somewhat redundant, that I answered this as well, but I was halfway there when I got notified things had been done already. –  Johannes Pille Apr 6 '13 at 20:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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