WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Whats the difference between these two code snippets, and which one should I use for a WordPress plugin?

require_once( '/inc/canagan_admin_functions.php' );



share|improve this question
The first one, because the second one won't work. – Otto Nov 30 '11 at 2:12
up vote 7 down vote accepted

The first one is like saying...

Include the file found in the inc directory, above the directory where this file is located.

The second statement is saying...

in the server root (/) look in the inc folder for canagan_admin_functions.php and include it.

The first one will work, the second won't. In the second you're looking in the root of the entire file system, where you file certainly won't be.

As a side note, whenever I start a plugin that's going to span multiple files, and/or enqueues CSS or JS files, I usually set up a few constant to help me along the way. Here's an example.

define( 'CD_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'CD_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

plugin_dir_path and plugin_dir_url are nice because they always return paths/urls with trailing slashes.

Then to include/require files, I use the constants. Keeps from having to call dirname(__FILE__) on every include.

You can then use the constants to include files...

require_once( CD_PLUGIN_PATH . 'path/to/file.php' );

or to enqueue scripts/styles...

add_action( 'wp_enqueue_scripts', 'wpse35088_enqueue' );
function wpse35088_enqueue()
    wp_enqueue_script( 'wpse35088-script', CD_PLUGIN_URL . 'path/to/script.js' );
share|improve this answer
The perfect answer! Thankyou :) – Shoebox Nov 30 '11 at 2:37

Your Answer


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.