How to Get the Current Page URL in WordPress?

How to Get the Current Page URL in WordPress?

While working on WordPress, sometimes, we need to get the current page URL. Here is the piece of code which you can place inside any PHP template file in your WordPress website.

There are two ways to get the current page URL in WordPress:

1. With base URL

2. Without base URL

Here is the piece of code which you can place inside any PHP template file in your WordPress website. Just copy the following code and paste it on Appearance » Editor »  functions.php file.

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

The above code lets you grab the URL with base URL or prefix (HTTP, HTTPS). For Example, page URL is https://tech-banker.com/how-to-get-the-current-page-url-in-wordpress/, above code will return https://tech-banker.com

If you want to get the current page URL without base URL, use the following code:

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

Hope it works!