Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

WordPress Install Root URL

Provides the top-level URL for a WordPress install, which is in some cases different than home_url(). This snippet will return the correct URL in any case.

  /**
   * finds the WP base URL
   * 
   * this can be different from the home url if wordpress is in a different directory (http://site.com/wordpress/)
   * 
   * this is to accomodate alternate setups
   * 
   * @return string
   */
  function app_base_url() {
    $scheme = parse_url(site_url(), PHP_URL_SCHEME) . '://';
    $content_path = explode( '/', str_replace( $scheme, '', content_url() ) );
    $wp_app_path = explode('/', str_replace( $scheme, '', site_url() ) );
    
    
    $end = min(array(count($content_path), count($wp_app_path)));
    $i = 0;
    $common = array();
    while ($i < $end and $content_path[$i] === $wp_app_path[$i]) {
      $common[] = $content_path[$i];
      $i++;
    }
    return $scheme . trailingslashit(implode('/', $common));
  }