Radek
Radek

Reputation: 11121

how to get wp include directory?

I need to do require_once for my wp plugin development. It seems to me that I need to use absolute path.

my current solution is

$delimiter = strpos(dirname(__FILE__), "/")!==false?"/":"\\"; //win or unix?
$path = explode($delimiter,  dirname(__FILE__));

require_once join(array_slice($path,0,count($path)-3),$delimiter) . "/wp-admin/includes/plugin.php"; 

I wonder if there is a better way to handle this, kind of general approach.

What if the wp plugin directory structure changes. So this part count($path)-3 won't be valid any more ....

Upvotes: 8

Views: 22654

Answers (5)

Petah
Petah

Reputation: 46040

Try:

require_once realpath(__DIR__.'/../../..').'/wp-admin/includes/plugin.php';

Or replace __DIR__ with dirname(__FILE__) if you are on < PHP 5.3

Or you could try:

require_once ABSPATH . WPINC . '/plugin.php';

Upvotes: 17

Martin Zeitler
Martin Zeitler

Reputation: 76679

I used to define it as a constant, like the others ...

if(defined('WP_CONTENT_DIR') && !defined('WP_INCLUDE_DIR')){
   define('WP_INCLUDE_DIR', str_replace('wp-content', 'wp-includes', WP_CONTENT_DIR));
}

One still could check with is_dir() and traverse further, if required.

Upvotes: 2

Benjamin J. Balter
Benjamin J. Balter

Reputation: 436

WordPress establishes the path to the includes directory on startup.

require_once ABSPATH . WPINC . '/your-file.php';

Upvotes: 23

Ohmicron
Ohmicron

Reputation: 31

Its possible to take advantage of WP_CONTENT_DIR and make a nice constant

define(WP_INCLUDE_DIR, preg_replace('/wp-content$/', 'wp-includes', WP_CONTENT_DIR));

or a function

function wp_include_dir(){
    $wp_include_dir = preg_replace('/wp-content$/', 'wp-includes', WP_CONTENT_DIR));
    return $wp_include_dir;
}

Upvotes: 3

Dan LaManna
Dan LaManna

Reputation: 3501

Have you tried doing bloginfo('wpurl') / wp-content/plugins ?

Upvotes: 2

Related Questions