wp_guess_url

The WordPress Core wp guess url function.

Description

wp_guess_url(); 

Usage

  1. if ( !function_exists( 'wp_guess_url' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/noop.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_guess_url(); 
  7.  

Defined (2)

The function is defined in the following location(s).

/wp-admin/includes/noop.php  
  1. function wp_guess_url() {} 
/wp-includes/functions.php  
  1. function wp_guess_url() { 
  2. if ( defined('WP_SITEURL') && '' != WP_SITEURL ) { 
  3. $url = WP_SITEURL; 
  4. } else { 
  5. $abspath_fix = str_replace( '\\', '/', ABSPATH ); 
  6. $script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] ); 
  7.  
  8. // The request is for the admin 
  9. if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) { 
  10. $path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] ); 
  11.  
  12. // The request is for a file in ABSPATH 
  13. } elseif ( $script_filename_dir . '/' == $abspath_fix ) { 
  14. // Strip off any file/query params in the path 
  15. $path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] ); 
  16.  
  17. } else { 
  18. if ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) { 
  19. // Request is hitting a file inside ABSPATH 
  20. $directory = str_replace( ABSPATH, '', $script_filename_dir ); 
  21. // Strip off the sub directory, and any file/query params 
  22. $path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] ); 
  23. } elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) { 
  24. // Request is hitting a file above ABSPATH 
  25. $subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) ); 
  26. // Strip off any file/query params from the path, appending the sub directory to the install 
  27. $path = preg_replace( '#/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] ) . $subdirectory; 
  28. } else { 
  29. $path = $_SERVER['REQUEST_URI']; 
  30.  
  31. $schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet 
  32. $url = $schema . $_SERVER['HTTP_HOST'] . $path; 
  33.  
  34. return rtrim($url, '/');