Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

WordPress Install Root Path

Finds the topmost directory of a WordPress install, even in unusual directory schemes.

/**
   * finds the WP installation root
   * 
   * this uses constants, so it's not filterable, but the constants (if customized) 
   * are defined in the config file, so should be accurate for a particular installation
   * 
   * this works by finding the common path to both ABSPATH and WP_CONTENT_DIR which 
   * we can assume is the base install path of WP, even if the WP application is in 
   * another directory and/or the content directory is in a different place
   * 
   * @return string
   */
  function app_base_path() {
    $content_path = explode('/', WP_CONTENT_DIR);
    $wp_app_path = explode('/', ABSPATH);
    $end = min(array(count($content_path), count($wp_app_path)));
    $i = 0;
    $common = array();
    while ($content_path[$i] === $wp_app_path[$i] and $i < $end) {
      $common[] = $content_path[$i];
      $i++;
    }
    return trailingslashit(implode('/', $common));
  }