5

This is a rewritten post since the way I worded my old post totally confused everyone.

MikeSchinkel shows how he uses Include files in his theme's functions.php file In this Stackexchange post: Organizing Code in your WordPress Theme's functions.php File?

Example:

require_once('includes/my-file.php');

How would you write this php line if the file to include is inside a directory, inside the wp-content directory? Location: [wordpress install]/wp-content/new-directory/my-file.php

The only way I can think of is like this:

require_once( ABSPATH . '/wp-content/new-directory/my-file.php' );

Would this be the correct way?

2 Answers 2

9

Including a file inside a subdirectory is the same as any other as long as you know the path to the file. Since it's possible to move the 'wp-content' directory, don't pass that part of the path to the require statement; instead, use the WP_CONTENT_DIR constant.

require_once WP_CONTENT_DIR . '/new-directory/my-file.php';

Also, you can leave out the parentheses since include and require are statements, not functions.

1
-1

This also works:

require_once( ABSPATH . 'wp-content/new-directory/my-file.php' );

or

require_once get_stylesheet_directory() . '/my-file.php';
1
  • Your first example assumes that the WordPress content dir is still named wp-content, which is not guaranteed.
    – Pat J
    Commented Sep 17 at 19:59

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.