How To Get Current Page Url In WordPress

In this blog post, we will discuss how to get the current page URL in WordPress. This can be useful for various reasons, such as adding social sharing buttons, generating dynamic content based on the URL, or tracking user engagement on your website.

Method 1: Using the get_permalink() function

The simplest way to get the current page URL in WordPress is by using the built-in get_permalink() function. This function returns the permalink (the full URL) of a given post or page. You can use this function in your theme’s template files, like this:

$current_page_url = get_permalink();
echo $current_page_url;

If you want to use it inside a loop, you can pass the post ID as an argument:

$current_page_url = get_permalink( get_the_ID() );
echo $current_page_url;

Method 2: Using the $_SERVER variable

To get the current page URL in WordPress without relying on any specific function, you can use the $_SERVER variable. This variable contains information about the server environment, such as the requested URL, the client’s IP address, and the server’s hostname. Here’s an example of how to get the current page URL using the $_SERVER variable:

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https://' : 'http://';
$current_page_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $current_page_url;

Method 3: Using the home_url() and add_query_arg() functions

Another way to get the current page URL in WordPress is by combining the home_url() and add_query_arg() functions. The home_url() function retrieves the home URL for the current site, while add_query_arg() appends the query string to a given URL. Here’s how to get the current page URL using these functions:

$current_page_url = home_url( add_query_arg( array(), $wp->request ) );
echo $current_page_url;

This method is useful when you want to include the query string in the URL, which can be helpful when you need to maintain the state of a specific user interaction or filter.


Getting the current page URL in WordPress can be achieved using various methods, such as the get_permalink() function, the $_SERVER variable, or a combination of home_url() and add_query_arg(). Choose the method that best suits your needs and requirements. Remember to always sanitize and validate the URLs before using them in your theme or plugin, especially if they are used as part of a user-generated input, to ensure the security of your website.