WordPress Path and URL Constants: plugin_dir_path, get_template_directory, home_url Reference

Paths and URLs in WordPress: plugin_dir_path(), get_stylesheet_directory() and friends

A plugin or theme needs two kinds of locations: file system paths for PHP (require, file_exists(), reading files) and URLs for the browser (stylesheets, scripts, images, links). Mixing them up, or hard-coding /wp-content/, is a typical source of bugs that appear only on another server, with WordPress in a subfolder or with a child theme.

Problem: A plugin includes its PHP files, enqueues its CSS, writes into uploads and builds links, and it breaks on a site where WordPress lives in a subdirectory or wp-content was moved.

Solution: Never hard-code paths. Use plugin_dir_path() / plugin_dir_url() for plugin files, the get_stylesheet_* and get_template_* functions for theme files, wp_upload_dir() for uploads, and home_url(), site_url(), admin_url() for links.

Plugin files

<?php
// in the main plugin file
define( 'HA_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); // /var/www/html/wp-content/plugins/my-plugin/
define( 'HA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );   // https://example.com/wp-content/plugins/my-plugin/

require_once HA_PLUGIN_PATH . 'includes/class-ha-plugin.php';                 // a path for PHP

wp_enqueue_style( 'ha-plugin', HA_PLUGIN_URL . 'assets/style.css', array(), '1.0' ); // a URL for the browser

Both functions return the folder of the file you pass, with a trailing slash. Called in includes/, they return the includes/ folder, which is why the constants are defined once in the main file.

Theme files

<?php
get_stylesheet_directory();      // the active theme: the child theme if one is used
get_stylesheet_directory_uri();
get_template_directory();        // always the parent theme
get_template_directory_uri();

// Look in the child theme first, then the parent
$file = locate_template( 'config/colors.json' );

Use get_template_* for files that belong to the parent theme itself, get_stylesheet_* for files a child theme may replace.

Uploads

<?php
$upload = wp_upload_dir();

$upload['basedir']; // /var/www/html/wp-content/uploads
$upload['baseurl']; // https://example.com/wp-content/uploads
$upload['path'];    // basedir + the current month folder, e.g. /2020/02
$upload['url'];     // the same as a URL

$dir = trailingslashit( $upload['basedir'] ) . 'my-plugin';
wp_mkdir_p( $dir ); // creates the folder with all parents if needed

In Multisite every site has its own uploads folder, and wp_upload_dir() returns the right one.

Constants

<?php
ABSPATH;         // /var/www/html/  (the WordPress folder, WITH a trailing slash)
WP_CONTENT_DIR;  // /var/www/html/wp-content  (no trailing slash)
WP_PLUGIN_DIR;   // /var/www/html/wp-content/plugins
WPMU_PLUGIN_DIR; // /var/www/html/wp-content/mu-plugins

For URLs prefer the functions content_url(), plugins_url() and includes_url() to the constants like WP_CONTENT_URL: the functions return the correct scheme (http or https) for the current request.

Links

<?php
home_url( '/contact/' );            // the site's address: https://example.com/contact/
site_url( '/wp-login.php' );        // the WordPress address: may be https://example.com/wordpress/wp-login.php
admin_url( 'options-general.php' ); // https://example.com/wp-admin/options-general.php
get_bloginfo( 'name' );             // the site title

NOTE: When WordPress is installed in a subdirectory but the site is served from the root, home_url() and site_url() differ. Links for visitors always use home_url(); site_url() is only for WordPress’s own files such as wp-login.php. get_bloginfo( 'url' ) and get_bloginfo( 'wpurl' ) return the same two values, but the functions are clearer.