How can I get the current path of my page?

I know how to do it inside a template file, but I need it inside functions.php. And it has to be before the send_headers hook, because I'll use this info to set a cookie.

I tried using PHP, but I'm getting /wp-admin

  • 1
    Do you mean parse_url( $_SERVER['REQUEST_URI'] )? – fuxia Apr 16 '13 at 10:28
  • 1
    Do you want to get path or URl of page? – Vinod Dalvi Apr 16 '13 at 10:29
//get current page's path
$pagePath = parse_url( $_SERVER['REQUEST_URI'] );
$pagePath = $pagePath['path'];
$pagePath = substr($pagePath, 1, -1);//remove slashes
| improve this answer | |
  • 1
    Rather use trim to remove slashes. – Fleuv Mar 6 '18 at 20:57
  • Or there's a WordPress function untrailingslashit. (Which uses rtrim.) – Rup Jan 9 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.