Get Page URL In WordPress: In 2 Steps

There may come a time when you, as a web developer or WordPress site administrator, need to know what the URL of the currently displayed page is.

This can help you find out where on your site people are coming from and give you ideas for content to put there. Not only that, but it’s a great tool for figuring out why 404 pages aren’t working.

No matter why you want to know how to get the URL of the WordPress page that is currently loaded, you have come to the right place because I will give you the code you need in this tutorial.

global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );

To Obtain the URL Of The Currently Loaded Page

No matter what page is currently loaded, this snippet will return the URL for that page in WordPress. It doesn’t matter whether you’re looking for a specific WordPress template URL (for a single post, a page, the homepage, a category, a tag, a custom post type, or anything else), as all of these can be found here.

Get Page URL In WordPress

This code snippet can be dropped into any PHP template file on your WordPress site for instant customization.

function wp_get_current_url() {
return home_url( $_SERVER['REQUEST_URI'] );}
echo esc_url( wp_get_current_url());

Frequently Asked Questions

WordPress: How do I get the current page’s URL?

Put the code below into the template files of your WordPress theme or anywhere else on your site to get the URL of the current page.

global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );

How do I get the current post name in WordPress?

Using the following code, you can find out what post or page is currently being displayed in WordPress:

$pagename = $post->post_name;
echo $pagename;

How do I get the current page slug in WordPress?

Use the following code to retrieve the WordPress page slug at the current time:

global $wp;
$current_slug = add_query_arg( array(), $wp->request );
echo $current_slug; // Display the slug of page

Wrap Up

I’ve outlined two simple methods here for determining the URL of the current page on your WordPress site.

The first is to write your own PHP code, and the second is to use a preexisting feature in WordPress. Knowing that you’d need to know how to get the current page URL in various PHP templates (single.php, page.php, category.php, and so on), I demonstrated this for you.

I really hope this article was informative for you and that you found what you were looking for. You can get in touch with me via the contact form or leave a comment if you still have questions after reading the article.

Please let me know in the comments if there are any WordPress-related topics you’d like me to cover, and I’ll do my best to accommodate your requests. I appreciate you taking the time to read this.

Further Read:

  1. Tips for Choosing the Right Plumbing System
  2. What You Need to Get Started
  3. Tips for Unleashing Your Creative Side
  4. How to Create a Custom E-Learning App for the UAE