Skip to content

Instantly share code, notes, and snippets.

@druellan
Created July 13, 2017 12:52
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 druellan/cf52aa21901f942a8f420bfce790a170 to your computer and use it in GitHub Desktop.
Save druellan/cf52aa21901f942a8f420bfce790a170 to your computer and use it in GitHub Desktop.
WordPress get current URL
/**
* There is no method of getting the current URL in WordPress.
* Various snippets published on the Web use a combination of home_url and add_query_arg.
* However, none of them work when WordPress is installed in a subfolder.
* The method below looks valid. There is a theoretical chance of HTTP_HOST tampered, etc.
* However, the same line of code is used by the WordPress core, for example in
* @see wp_admin_canonical_url
* so we are going to use it, too
* *
* Note that #hash is always lost because it's a client-side parameter.
* We might add it using a JavaScript call.
*/
function wp_get_current_url() {
return set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment