11

Is the trailing slash '/' only difference between these two? If so, I can use trailingslashit(__DIR__)?

Venkat
  • 111
  • 1
  • 1
  • 5

3 Answers3

14

plugin_dir_url(__FILE__) This function provides you the url of the file directory.

plugin_dir_url(__DIR__) This function provides you the url plugins folder.

__FILE__ this magic constant will give you the path of file where the file is exist.

__DIR__ this magic constant will give you the path of directory where the file is exist.

trailingslashit(__DIR__) this function will return the path of directory and add shash after the path of directory.

plugin_dir_path(__FILE__). will give you same result as trailingslashit(__DIR__). and my suggestion to use plugin directory path because it is a wordpress way.

Rajkumar Gour
  • 1,131
  • 12
  • 26
11
  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.

more info : https://developer.wordpress.org/reference/functions/plugin_dir_path/

  1. 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

more info : https://codex.wordpress.org/Function_Reference/plugins_url

  1. 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.

more info : https://codex.wordpress.org/Function_Reference/plugin_dir_url

Shital Marakana
  • 2,817
  • 1
  • 9
  • 12
7

Lets untrail what is happening:

The wordpress function is as simple as this:

function plugin_dir_path( $file ) {
    return trailingslashit( dirname( $file ) );
}

So,

include plugin_dir_path(__FILE__) . 'xx.php';

Is equal to

include trailingslashit( dirname( __FILE__ ) ) . 'xx.php';

In PHP 5.3, __DIR__ was introduced as a replacement for dirname( __FILE__ ). If you don't need to support PHP < 5.3 (you don't), it can be reduced to:

include trailingslashit( __DIR__ ) . 'xx.php';

(also see this : Is there any difference between __DIR__ and dirname(__FILE__) in PHP?)

As __DIR__ doesn't return something with a trailing slash, there is no need to do the trailingslashit thing. So we can reduce further to:

include __DIR__ . '/xx.php';

So, to conclude, the following lines all does the exact same thing (on PHP >= 5.3):

include plugin_dir_path(__FILE__) . 'xx.php';
include trailingslashit( dirname( __FILE__ ) ) . 'xx.php';
include trailingslashit( __DIR__ ) . 'xx.php';
include __DIR__ . '/xx.php';

Which is best? I prefer the last one. You don't have to type as much, it is less noisy, and you don't have to worry about what magic is inside that plugin_dir_path function. And this is how you usually include files in PHP. Some priests may say you should do it the Wordpress way. Be a rebel!

rosell.dk
  • 2,228
  • 25
  • 15
  • 1
    You can define a constant in your main plugin/theme file to have the value of `__DIR__` and reuse that constant in other places, so you can be sure your includes & requires are always relative to the plugin/theme root folder. This is useful, because `__DIR__` will point to a different folder in every folder. It can cause issues if you move your files around for some reason. I just create a const `define( 'MYPLUGINNAME_BASEDIR', __DIR__ );` as first things, and reuse that everywhere. My IDE (PHPStorm) can also resolve this properly, making include / require inspections useful again. – Bence Szalai Jan 18 '19 at 15:15