Codex tools: Log in
Contents |
When coding WordPress plugins you often need to reference various files and folders throughout the WordPress installation and within your plugin or theme.
WordPress provides several functions for easily determining where a given file or directory lives. Always use these functions in your plugins instead of hard-coding references to the wp-content directory or using WordPress' internal constants.
It's important to remember that WordPress allows users to place their wp-content directory anywhere they want, so you must never assume that plugins will be in wp-content/plugins, or that uploads will be in wp-content/uploads, or that themes will be in wp-content/themes.
Related to the above, it's also important to note that PHP's __FILE__ magic-constant resolves symlinks automatically, so if the wp-content or wp-content/plugins or even the individual plugin directory is symlinked, this function will not work right.
If your plugin includes JavaScript files, CSS files or other external files, then it's likely you'll need the URL to these files so you can load them into the page. To do this you should use the plugins_url() function like so:
plugins_url( 'myscript.js', __FILE__ );
This will return the full URL to myscript.js, such as example.com/wp-content/plugins/myplugin/myscript.js.
To load your plugins' JavaScript or CSS into the page you should use wp_enqueue_script() or wp_enqueue_style() respectively, passing the result of plugins_url() as the file URL.
WordPress includes many other functions for determining paths and URLs to files or directories within plugins, themes, and WordPress itself. See the individual Codex pages for each function for complete information on their use.
plugins_url() plugin_dir_url() plugin_dir_path() plugin_basename()
get_template_directory_uri() get_stylesheet_uri() get_theme_root_uri() get_theme_root() get_theme_roots()
home_url()
admin_url() site_url() content_url() includes_url() wp_upload_dir()
get_admin_url() get_home_url() get_site_url() network_admin_url() network_site_url() network_home_url()
WordPress makes use of the following constants when determining the path to the content and plugin directories. These should not be used directly by plugins or themes, but are listed here for completeness.
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
// Available per default in MS, not set in single site install // Can be used in single site installs (as usual: at your own risk) UPLOADS // uploads folder, relative to ABSPATH (for e.g.: /wp-content/uploads)
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 above was used for backwards compatibility, but due to complications with WordPress websites Version 2.9 and below that are using SSL, should not be used. If you are using the code above in your plugins, you should replace it with the code below and remove the use of these global variables from your plugin.
if ( ! defined( 'WP_CONTENT_URL' ) ) define( 'WP_CONTENT_URL', WP_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' );
WordPress Directories:
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 |
theme_url() | Themes directory URL (#18302 - wp3.5) | http://www.example.com/wp-content/themes |
wp_upload_dir() | Upload directory URL (returned array) | http://www.example.com/wp-content/uploads |