Website owners and nearly all WordPress developers often need to get the current page URL (Uniform Resource Locator) for their projects. It doesn’t matter whether you’re viewing a post or a page, and if you need a category or tab template or a custom post form, the methods below have parameters and attributes that can get that done. You can also show only slugs in WordPress, depending on the permalink structure. Finally, WordPress has some inbuilt features that can let you show URL in specific templates such as home.php
and author.php
, and load those such as category.php
, tag.php
, and so on. Now, here’s how to get the current page URL in WordPress.
Code snippets for getting the URL of a current page
We’ll start with the WordPress loop, since many developers while testing things or doing projects, require the URL to be shown on the screen when the page loads in the browser. Others want to post shareable URLs of those pages or prepare custom URLs for actions or links. Regardless, the get_permalink()
function is the quickest solution for a single post or a page. You can add these to any PHP template file on your website (such as in theme files). That would look like this:
global $wp;
echo home_url( $wp->request )
In that example, $wp->request
signifies the data path, i.e., everything outside the website URL, while home_URL
shows the site UR from Settings → General in the WordPress Admin Section. Unfortunately, that won’t function if the Permalinks are configured to “Plain”. Instead, you may have to use add_query_arg()
and pass $wp->query_vars
through it. That situation would produce this outcome:
global $wp;
echo add_query_arg( $wp->query_vars, home_url() );
If we combine these two, we get this code that would work with any Permalink configuration:
global $wp;
echo add_query_arg( $wp->query_vars, home_url( $wp->request ) );
The only problem with the code above is that it won’t preserve query parameters in place. To do so, you must put $_GET
instead of array()
above. Some users came up with an even shorter solution that does the same thing:
$current_url = home_url($_SERVER['REQUEST_URI'])
Since many users also want to only show the slug, particularly that of the latest page or post, they can use this code:
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
Here’s a way to only show the current URL with zero parameters:
$ current_url = strtok ($ _ SERVER [“REQUEST_URI”], ‘?’);
If you want to show the URL on the front-end of your website, not only get it, add echo, so it looks like this:
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
echo $current_url;
Developers at isitwp.com
also composed this code (we’re unaffiliated, credits go to them). You need to edit the WordPress theme and put it inside the index.php
file, and only there:
<? PHP $ paged = (get_query_var (‘paged’))? get_query_var (‘paged’): 1; ?>
We also prepared examples for PHP code snippets when you need to make HTTP or HTTPS requests (or both). Let’s start with an HTTP/HTTPS request to get the current page URL in WordPress:
?php $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>
If you only need to send an HTTPS one, use this:
<?php $current_url="https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>
As for only the HTTP request, remove the s
from https://
in the code above.
Get current page URL in WordPress via a function
Although you can add the codes above inside your PHP templates and include them in the WordPress loop, sometimes you may need functions as a whole. Therefore, here’s one simple example of functions for showing the current URL of a page or post in WordPress:
<?php
function wp_get_current_url() {
return home_url( $_SERVER['REQUEST_URI'] );
}
If you need an example of using that PHP code in HTML, the result would be like this:
The URL of the current page is: <a href="<?php echo esc_url( wp_get_current_url()
) ?>"><?php echo esc_html( wp_get_current_url() ) ?></a>.
Use specific PHP templates to get URL of current pages or posts
The examples above apply to mostly all PHP templates, but while researching, we noticed some users want to use built-in WordPress features. That is commendable and the right way to go about it. Therefore, we’ll start with a common example where you can simply replace the first line. Then, we’ll demonstrate the use of $obj_id
in WordPress for some templates. They do the same job but vary in application, so choose whichever fits your needs. First, we want to show a feature that will return the same URL regardless of the page, not only the home page (home.php
) or front page (front-page.php
).
$current_url = home_url( '/' );
Now you can use this code snippet, and only replace the second line with examples below:
$queried_id = get_queried_object_id();
// put the line from below here
Now, use this line if your template uses page.php
or single.php
templates:
$current_url = get_permalink( $queried_id );
Enter this line if you load taxonomy URLs such as tags or categories:
$current_url = get_term_link( $queried_id );
Use the following archive page URL by author ID if you draw information from the author.php
file:
$current_url = get_author_posts_url( $queried_id );
Utilize object ID for current URL of posts or pages
Now for the promised second part. The first works when your template loads page.php
or single.php
files:
$obj_id = get_queried_object_id();
$current_url = get_permalink( $obj_id );
Now, we have a code for the current author archive, if author.php
is loaded at any point:
$obj_id = get_queried_object_id();
$current_url = get_author_posts_url( $obj_id );
Finally, we have a code you can use when loading taxonomies (taxonomy.php
) such as tags (tag.php
) or categories (category.php
).
$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );