0

When including files in a plugin, I've seen people do

WP_PLUGIN_DIR.'/'.dirname(plugin_basename(__FILE__)).'/somefile.css'

and yet WordPress wp_enqueue_script/style uses URI for the file path (such as plugins_url).

What difference is there? I'm confused...

2 Answers 2

2

When you include something, you use the file path because it's a local file and you're reading into the environment right here and now and using that code.

When you "enqueue" something, you're not reading the file in, you're sending the URL of the file to a system that puts that URL in the resulting output webpage, for the viewer's browser to then load and read.

You use a file path when the PHP code is manipulating the file. You use a URL when the browser needs to see that URL and retrieve the file itself.

3
  • Are you saying that there are 2 versions of the same file (say style.css)? What if I have my style.css installed locally? Why do I need to use the URI then?
    – Bobby
    Dec 1, 2013 at 1:54
  • For example, echo get_theme_root().'twentythirteen/style.css' VS echo get_stylesheet_uri(). Are they both linking to the same file?
    – Bobby
    Dec 1, 2013 at 2:06
  • Your question does not make any sense. get_theme_root() gives a local path, while get_stylesheet_uri() gives a URL. They connect to the same file, but in two different ways.
    – Otto
    Dec 2, 2013 at 5:29
1

Dir[ectory] refers to paths in local server file system. These must be used for accessing/manipulating files inside the program and are not (and should not) be used or visible to visitors on web page.

URL/URI refers to web-accessible paths as served by web server to visitors.

PS while technically PHP can include/require files from URLs as well it's insecure, poorly performing and shouldn't be used.

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.