How to Get the Current Page URL in WordPress

As a website owner, you may need to get the current page URL in your WordPress site. This article will cover several methods of how to get the current page URL in WordPress. They can help you view any current page URL, whether a single post, blog post, home page, custom post, etc. Get started!

Method 01: Using the get_permalink() Function

This method can help you view the current page URL of single posts, pages, and custom posts.

  • Use the following code in any PHP template file on your WordPress site.

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

  • But if you want to view only the latest WordPress (the remaining part, coming after the base URL of your web page), you can use this code.

/* For example if your website is “”,
it will return “first-post” */

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

Method 02: Using the wp_get_current_url Function

  • You can use this function by adding the following code to your PHP file.

function wp_get_current_url() {
return home_url( $_SERVER[‘REQUEST_URI’] );

You don’t have to worry about $_SERVER[‘REQUEST_URI’] is never null. Because WordPress calls wp_fix_server_vars() for any change to make sure it exits for these and other server variables. This helps in maintaining query string values as a perk. This makes it compatible with both the simple and custom permalink frameworks.

However, it’s important to note that using $_SERVER[‘REQUEST_URI’] can create a potential security vulnerability. This can create an insecure user interface for XSS (Cross-Site Scripting) attacks. It means you will have no escape from the above-mentioned feature usage.

But don’t worry. If you want to use the $_SERVER[‘REQUEST_URI’] feature in HTML attributes, you can ensure the safety of your code from any potential attack using the esc_url().

Example of How to Display Current Page URL on WordPress

Use the following code to display the current page URL on your WordPress site.

The current page is <a herf=”<?php echo esc_url( wp_get_current_url()
) ?>”><?php echo esc_html( wp_get_current_url() ) ?></a>

Bonus Point: How to Get WordPress Current Page URL on Specific PHP Templates

  • Use the following code if you load the single.php or page.php theme template file for a single post or URL page.

$obj_id = get_queried_object_id();
$current_url = get_permalink( $obj_id );

  • Use the code below if you load theme template files like taxonomy.php, category.php, tag.php, etc., to display the latest taxonomy concept URL like category or tag.

$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );

  • Use this code if you use the format author.php for showcasing the current author archive.

$obj_id = get_queried_object_id();
$current_url = get_author_posts_url( $obj_id );

  • Use this code for the homepage URL to return the same URL on all pages except the front-page.php and home.php.

$current_url = home_url( ‘/’ );

Final Words

We have covered almost everything you need to know on how to get the current page URL in WordPress. We have covered multiple methods, an example, several bonus points, and a video tutorial to simplify the process. If you still have any queries or suggestions for us, we request you to let us know through the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

Need help?