Created
January 24, 2010 05:14
-
-
Save felipelavinz/285026 to your computer and use it in GitHub Desktop.
simple function to relativize request to a WP site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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