When creating WordPress plugins, themes, or tweaking plugins, themes, functions.php, you will often need to reference files and folders throughout the WordPress installation. Since WordPress 2.6 (if they are pre-2.6, then use die();), users have had the ability to move this directory anywhere they want, so you always want to avoid hardcoding so that the code is re-usable, especially with plugins. WordPress has defined a set of PHP CONSTANTS (by the way, constants are always UPPERCASE) to store the path to the wp-content and plugins directories. You can use these CONSTANTS in your plugins, child themes or functions.php for any paths you need regardless of where the actual directory might exist on the server.
- WP_CONTENT_URL: Full URL to wp-content
- WP_CONTENT_DIR: The server path to the wp-content directory
- WP_PLUGIN_URL: Full URL to the plugins directory
- WP_PLUGIN_DIR: The server path to the plugins directory
- WP_LANG_DIR: The server path to the language directory
You can set your own CONSTANT_DIR by (as a reminder, constants are always UPPERCASE):
[php]define('CONSTANT_DIR', ABSPATH . 'wp-content'); //sets DIR[/php]
You can set your own CONSTANT_URL by (as a reminder, constants are always UPPERCASE):
[php]define('CONSTANT_URL', get_option('siteurl').'/wp-content/'); //sets URL[/php]
For GENESIS Users, some CONSTANTS of interests are:
- STYLESHEETPATH: /.../.../.../..../.../.../wp-content/themes/child_theme
- TEMPLATEPATH: /.../.../.../..../.../.../wp-content/themes/genesis
- PARENT_URL: get_bloginfo(‘template_directory’): e.g., http://domain.com/wp-content/themes/genesis
- CHILD_URL: get_bloginfo(‘stylesheet_directory’): e.g., http://domain.com/wp-content/themes/child_theme
- PARENT_THEME_VERSION: refers to the Genesis version running
- PARENT_THEME_NAME = 'Genesis'
- GENESIS_LANGUAGES_URL (sets to Genesis Library for Child Themes)
For THESIS (1.8) Users, some CONSTANTS of interests are:
- THESIS_CUSTOM = STYLESHEETPATH: /.../.../.../..../.../.../wp-content/themes/thesis_18/custom
- THESIS_CUSTOM_FOLDER: get_bloginfo('stylesheet_directory') . '/custom': e.g., http://domain.com/wp-content/themes/thesis_18/custom
- THESIS_LAYOUT_CSS: THESIS_CUSTOM . '/layout.css: e.g., http://domain.com/wp-content/themes/thesis_18/layout.css
- THESIS_ROTATOR:THESIS_CUSTOM . '/rotator': e.g., http://domain.com/wp-content/themes/thesis_18/rotator
- THESIS_ROTATOR_FOLDER: THESIS_CUSTOM_FOLDER . '/rotator': e.g., http://domain.com/wp-content/themes/thesis_18/rotator
Are there any CONSTANTS that are used quite often that I missed?
hamza khan says
thanks useful
Dave says
Travis,
That’s a very nice list, thanks so much!
I’m wondering if there’s one for the site root of a Genesis install? Or plain WordPress install, for that matter? Like most, I find myself frequently working on temp URL’s on some server, and it would be nice to avoid changing the funny file path that this makes for some items.
I just ran into the handy idea of setting the site root in wp-config so that you could use plain old “/” as your root for this kind of stuff.
Your thoughts?
Thanks, Dave
Travis Smith says
I am not sure I follow you. When dealing with images, etc., I create and use a shortcode like [ wpurl] that you can find in my Genesis Shortcodes plugin.