Skip to content

Instantly share code, notes, and snippets.

@chuckreynolds
Created February 26, 2016 06:38
Show Gist options
  • Save chuckreynolds/1f49cb8edcf41debc61c to your computer and use it in GitHub Desktop.
Save chuckreynolds/1f49cb8edcf41debc61c to your computer and use it in GitHub Desktop.
testing better way to get the current url in wordpress. resourced from: https://roots.io/routing-wp-requests/
<?php
function get_current_url() {
// Get current URL path, stripping out slashes on boundaries
$current_url = trim(esc_url_raw(add_query_arg([])), '/');
// Get the path of the home URL, stripping out slashes on boundaries
$home_path = trim(parse_url(home_url(), PHP_URL_PATH), '/');
// If a URL part exists, and the current URL part starts with it...
if ($home_path && strpos($current_url, $home_path) === 0) {
// ... just remove the home URL path form the current URL path
$current_url = trim(substr($current_url, strlen($home_path)), '/');
}
return $current_url;
}
$current_url = get_current_url();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment