Website Root

  home_url()	
  //  https://www.example.com

  get_home_path()
  //  /var/www/vhosts/example.com/httpdocs/

WordPress root

Get URL for the current site including the sub directory if WordPress is not installed in the domains root directory (e.g. to where “wp-blog-header.php” and “wp-admin/” are located).

  $SiteUrl = site_url();
  // http://www.example.com or http://www.example.com/wordpress


WordPress Theme

get_stylesheet_directory_uri() is better because get_template_directory_uri() will get the main theme, not the child theme if being used

Return full URI path (including https://)
$MyImagePath = get_stylesheet_directory_uri() . "/mysubfolder/myimage.jpg";
Return path on local server

(e,g, /home/user/public_html/wp-content/themes/my_theme)

  if (!is_file(get_stylesheet_directory() . "/mysubfolder/myimage.jpg"))
  {
  }
Themes directory
  theme_url()
  //  https://www.example.com/wp-content/themes
Other theme path functions
get_template_directory_uri()
get_stylesheet_uri()
get_theme_root_uri()
get_theme_root()
get_theme_roots()
get_template_directory()

WordPress Plugin

Plugin’s root directory
  plugins_url()
  //  https://example.com/wp-content/plugins
This plugin

URL

  plugin_dir_url( __DIR__ )
  //  https://example.com/wp-content/plugins/my-plugin-name/
  //THIS DOESNT'T ALWAYS SEEM TO WORK, SOMETIMES NEED TO USE FILE INSTEAD - CHECK BEFORE YOU COMMIT

  plugin_dir_url( __FILE__ )
  //  https://example.com/wp-content/plugins/my-plugin-name/folder-this-file-is-in/

Filesystem path

  plugin_dir_path( __DIR__ )
  //  /var/www/vhosts/example.com/httpdocs/wp-content/plugins/my-plugin_name/

  plugin_dir_path( __FILE__ )
  //  /var/www/vhosts/example.com/httpdocs/wp-content/plugins/my-plugin_name/folder-this-file-is-in/
Path relative to wordpress plugins root directory
  plugin_basename( __DIR__ )
  //  my-plugin_name/folder-this-file-is-in
    
  plugin_basename( __FILE__ )
  //  my-plugin_name/folder-this-file-is-in/this-files-name.php

The current file

Filesystem path
  plugin_dir_path( __DIR__ )
  //  /var/www/vhosts/example.com/httpdocs/wp-content/plugins/my-plugin_name/

  echo plugin_dir_path( __FILE__ );
  //Example output
  //  /var/www/vhosts/my-domain-name.com/httpdocs/wp-content/plugins/my-plugin_name/the-directory-this-file-is-in/

(The “plugin” part of the name is misleading – it can be used for any file, plugin file or not)

Filesystem path – web server referenced
  echo ( __DIR__ );
  //Example output
  //  /home/websiteusername/public_html/wp-content/plugins/my-plugin_name/the-directory-this-file-is-in

  echo ( __FILE__ );
  //Example output
  //  /home/websiteusername/public_html/wp-content/plugins/my-plugin_name/the-directory-this-file-is-in/this-file-name.php

Upload directory

If use date based folder is enabled for your wordpress site then a folder will be created for the current year/month if it doesn’t exist already

  $UploadDirectory = wp_upload_dir();
  //  Returns an array:
  //  $UploadDirectory['path']  /var/www/vhosts/example.com/httpdocs/wp-content/uploads
  //  $UploadDirectory['url']  https://example.com/wp-content/uploads 
  //  $UploadDirectory['subdir']  
  //  $UploadDirectory['basedir']  /var/www/vhosts/example.com/httpdocs/wp-content/uploads
  //  $UploadDirectory['baseurl']  https://example.com/wp-content/uploads 
  //  $UploadDirectory['error']  
Creating your own folder in the upload directory
  $UploadDirectory = wp_upload_dir(); 
  $OurUploadDirectory = $UploadDirectory['basedir'] . '/my-folder-name';
  if (!file_exists($OurUploadDirectory))
    wp_mkdir_p($OurUploadDirectory);

Other WordPress Directories

Admin directory
  admin_url()
  //  https://www.example.com/wp-admin
Includes directory
  includes_url()
  // https://www.example.com/wp-includes
Content directory
  content_url()
  //  https://www.example.com/wp-content
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.