“Determining Plugin and Content Directories”的版本间的差异

跳转至: 导航搜索
The Code: Removed check for SITEURL. It was redundant -- already done in get_option()

2009年3月14日 (六) 09:23的版本

When coding WordPress plugins you often need to reference various files and folders throughout the WordPress installation. This becomes very complicated because with each new version of WordPress users are able to control where their folders live with more detail. In version 2.6, users were given the ability to move their /wp-content/ directory to anywhere they want, and many users already keep all WordPress files (like /wp-admin/ and /wp-includes/) in an unusual place.

To get around these different settings, as well as to remain compatible with versions from before these new settings were in place, it's useful to check and define various PHP constants within your plugin and always use those constants rather than depending on any given WordPress install to behave as you expect.

The Code

The following code should give you backwards-compatible values for:

  • WP_CONTENT_URL - previously this was always the blog's url + /wp-content/
  • WP_CONTENT_DIR - this is the server path to the wp-content directory.
  • WP_PLUGIN_URL - the url of the plugins directory (usually inside wp-content)
  • WP_PLUGIN_DIR - the server path to the plugins directory
// Pre-2.6 compatibility
if ( ! defined( 'WP_CONTENT_URL' ) )
      define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
if ( ! defined( 'WP_CONTENT_DIR' ) )
      define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_URL' ) )
      define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
      define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );