wordpress

Determining Plugin and Content Directories


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, you should always use the appropriate functions.

Available Functions

In Version 1.5, functions were defined to retrieve special paths.

plugin_basename()
get_theme_root()
get_theme_root_uri()

In Version 2.6, functions were defined to retrieve special URLs.

site_url()
admin_url()
content_url()
plugins_url()
includes_url()

In Version 2.9, the following function was defined to retrieve the themes roots.

get_theme_roots()

In Version 3.0, the following function was defined to retrieve the home URL.

home_url()

In Version 3.0, these functions retrieve URLs of another site in multisite (or the network, in the case of network_functions).

get_site_url()
get_home_url()
get_admin_url()
network_site_url()
network_home_url()
network_admin_url()

Also, do not forget that there are some handy constants: (NOTE: These constants are *not SSL aware*. Apparently they are intended for Core use, not Plugins. Plugins should use the above functions instead, or use the compatibility code in the next section.)

WP_CONTENT_DIR  // no trailing slash, full paths only
WP_CONTENT_URL  // full url 

WP_PLUGIN_DIR  // full path, no trailing slash
WP_PLUGIN_URL  // full url, no trailing slash

Backwards-compatibility Code

Use the code below if you want to stay compatibile with Version 2.9 and older:

 if ( ! function_exists( 'is_ssl' ) ) {
  function is_ssl() {
   if ( isset($_SERVER['HTTPS']) ) {
    if ( 'on' == strtolower($_SERVER['HTTPS']) )
     return true;
    if ( '1' == $_SERVER['HTTPS'] )
     return true;
   } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
    return true;
   }
   return false;
  }
 }

 if ( version_compare( get_bloginfo( 'version' ) , '3.0' , '<' ) && is_ssl() ) {
  $wp_content_url = str_replace( 'http://' , 'https://' , get_option( 'siteurl' ) );
 } else {
  $wp_content_url = get_option( 'siteurl' );
 }
 $wp_content_url .= '/wp-content';
 $wp_content_dir = ABSPATH . 'wp-content';
 $wp_plugin_url = $wp_content_url . '/plugins';
 $wp_plugin_dir = $wp_content_dir . '/plugins';
 $wpmu_plugin_url = $wp_content_url . '/mu-plugins';
 $wpmu_plugin_dir = $wp_content_dir . '/mu-plugins';

Previously, the code below was used for backwards compatibility, but due to complications with WordPress websitesVersion 2.9 and below that are using SSL, should not be used. If you are using the code below in your plugins, you should replace it with the code above and remove the use of these global variables from your plugin.

 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' );
 if ( ! defined( 'WPMU_PLUGIN_URL' ) )
       define( 'WPMU_PLUGIN_URL', WP_CONTENT_URL. '/mu-plugins' );
 if ( ! defined( 'WPMU_PLUGIN_DIR' ) )
       define( 'WPMU_PLUGIN_DIR', WP_CONTENT_DIR . '/mu-plugins' );

Related

home_url() Home URL http://www.example.com
site_url() Site directory URL http://www.example.com OR http://www.example.com/wordpress
admin_url() Admin directory URL http://www.example.com/wp-admin/
includes_url() Includes directory URL http://www.example.com/wp-includes/
content_url() Content directory URL http://www.example.com/wp-content/
plugins_url() Plugins directory URL http://www.example.com/wp-content/plugins/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s