开发者

function, constant or variable that holds full path to WP directory?

开发者 https://www.devze.com 2023-01-29 05:59 出处:网络
assuming that i\'m writing a wordpress plugin. now i want its includes and include includes to be relative to the wordpress directory s开发者_如何学Goo the question is there a global variable/constant

assuming that i'm writing a wordpress plugin. now i want its includes and include includes to be relative to the wordpress directory s开发者_如何学Goo the question is there a global variable/constant or a function to retreive this info ?


Yes you can do ABSPATH example: require_once(ABSPATH . 'wp-settings.php'); This will give you the absolute path to your WordPress Installation.


It is important if you are writing a plugin for public distribution, that you make sure that you do not hard code any of the paths. This is one of the biggest problems plugin developer's cause, because users have the ability to move these folders around.

The basic way to get URLs to the site is: get_bloginfo('url'); <- goes to sites root get_bloginfo('wpurl'); <- goes to wordpress' root which can be two different things. So this is a good way to get to the wordpress directory.

There are also defined variables: WP_PLUGIN_URL WP_PLUGIN_DIR

A way suggested in the codex to get right to your plugin's folder for includes is this:

WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__));

Should output: http://[url-path-to-plugins]/[myplugin]/

No matter where the user places their plugins folder.


There's the get_bloginfo function which can be used to retrieve the full URL for your WordPress install, your blog and/or your theme directory.

To get your plugin's directory, take a look at the plugin_basename function.

0

精彩评论

暂无评论...
验证码 换一张
取 消