WordPress root

Homepage URL

  $HomeUrl = home_url();
  // "https://www.example.com" or "https://www.example.com/mywordpressfolder"

  $HomeUrl = home_url('/');
  // "https://www.example.com/" or "https://www.example.com/mywordpressfolder/"

  $HomeUrl = home_url('/mypage');
  // "https://www.example.com/mypage" or "https://www.example.com/mywordpressfolder/mypage"
Reirect to a page
  wp_redirect( home_url( '/' ) );
  //wp_redirect( home_url( '/my_page_name' ) );    //If you want to redirect to a specific page
  die();
Site WP install URL

The URL of the WordPress install. Usually identical to using site_url(), but not necessarily! User home_url() generally.

  $SiteUrl = site_url();
  // "https://www.example.com" or "https://www.example.com/mywordpressfolder"

  $SiteUrl = site_url('/');
  // "https://www.example.com/" or "https://www.example.com/mywordpressfolder/"

  $SiteUrl = site_url('/mypage');
  // "https://www.example.com/mypage" or "https://www.example.com/mywordpressfolder/mypage"
URL to WP admin area

For ap-admin you should use site_url(), tahts the correct usage!

  $AdminAreaUrl = site_url('/wp-admin');

WordPress root – Server filesystem

  $SiteRootFilePath = ABSPATH;
  //  /var/www/vhosts/example.com/httpdocs/
  //or for a wordpress site installed in a sub directory:
  //  /var/www/vhosts/example.com/httpdocs/mysubdirectory/

  get_home_path()      <<This is a backend function, it only works if the necessary wordpress include files are present. ABSPATH is usually better (its a define that just works)
  //  /var/www/vhosts/example.com/httpdocs/

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
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *