Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created January 24, 2010 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipelavinz/285026 to your computer and use it in GitHub Desktop.
Save felipelavinz/285026 to your computer and use it in GitHub Desktop.
simple function to relativize request to a WP site
<?php
/**
* Return the request path, relative to the WP base URL
* @param boolean $return_parts Return as a string or an array
* @return string|array The relative request OR array with relative "directories"
*/
function relativize_url($return_parts=true){
$wp_url = get_bloginfo('url'); //base URL for this WordPress site
$wp_url_parts = parse_url($wp_url); //
$protocol = ( $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
$full_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // the complete URL for this request
$url_parts = parse_url($full_url);
$request_path = str_replace($wp_url_parts['path'], '', $url_parts['path']); // request path, relative to the WP install
if ( $return_parts) return array_values(array_filter(explode('/', $request_path))); // decomposed relative request path
else return $request_path;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment