What should I use instead of WP_CONTENT_DIR and WP_PLUGIN_DIR?

Reference file in current path or deeper nested

To reference the current path

plugin_dir_path( __FILE__ )."further/nesting/here.css";

which works in Plugins and Themes.

Reference URl/URi in a plugin

To point to a plugin or theme file, use

plugins_url( "path/to/file", __FILE__ );

which works only in plugins

Reference URl/URi in wp-admin folder

Always point them to admin_url( 'some/path' );. There is get_admin_url() as well.

Reference URl/Uri in wp-includes folder

Point them to includes_url( 'some/path' );

An URl/URi relative to the sites home

There’s home_url( 'etc' ); or get_home_url() for that. Similar is get_site_url() and site_url(). Then there as well is network_home_url(). And you got network_admin_url()

wp-content or the renamed directory

You can redefine the wp-content folder name. Therefore you use content_url() there.

How to get the plugins folder url?

If using WP_PLUGIN_DIR is wrong, what is the suggested alternative?

Simply use plugins_url() without any parameters.

If you use it for a plugin, it works for MU plugins too.

EDIT #1 If you’re interested in the path to the current plugin, use plugin_basename();.

EDIT #2 If you’re interested in all sidewide active plugins, use wp_get_active_network_plugins();.

If you’re not on multisite, go with wp_get_active_and_valid_plugins();. This will take multisite/network into account. Keep in mind that this will take them into account if you’re not on multisite, but got a sunrise.php dropin.

You could as well retrieve them via get_option( 'active_plugins' );, which is not recommended as it doesn’t take filters and plugin validation into account, which happens on several occasions.


Further reading

This list goes on and on. Take a look at the search results on QueryPosts.com for more info.

This article goes in absolute depth about all paths.

Leave a Comment