How to Get the Current Page URL in WordPress

Here’s a helpful solution that helps you to quickly WordPress get current page URL, regardless of the page being viewed. If you need a single post URL, blog URL, home page, category template, tag template, custom post form, or some other WordPress template, it doesn’t matter.

WordPress Get Current Page URL (Easy Methods)

WordPress Get Current Page URL

In most circumstances, get_permalink() covers your requirements only with the current page URL for single posts, pages, or custom posting types.

In any PHP template file on your WordPress site you can use this piece of code:

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

But also if you want to get just the latest WordPress slug page (last part without your simple URL), this is the code:

/* For example if your website is "https://YourWebsiteName.com/first-post",
it will return "first-post" */

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

WordPress Get Current URL Function

<?php
function wp_get_current_url() {
return home_url( $_SERVER['REQUEST_URI'] );
}

You don’t have to think about the $_SERVER[‘REQUEST_URI’] is never null, as WordPress calls any charge of wp_fix_server_vars() to ensure it exists for these and other server variables. This keeps query string values as a perk, meaning that it fits with simple and custom permalink frameworks.

Please notice that you can not escape the above-mentioned feature usage, since $_SERVER[‘REQUEST_URI’] can provide an unsafe user interface for XSS (Cross-Site Scripting) attacks. Don’t worry, if used in HTML attributes and esc_url(), this is an easy way to transfer the output to esc_url().

Here is Current Page URL Example Usage

The current page is <a href="<?php echo esc_url( wp_get_current_url()
) ?>"><?php echo esc_html( wp_get_current_url() ) ?></a>.

Bonus Content: WordPress Get Current Page URL on Specific PHP Templates

As an addition, WordPress has its own features, based on a website currently displayed. Let’s look at the following cases.

When single.php or page.php theme template file is loaded for a single post or URL page at this time:

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

For the latest taxonomy concept ( e.g. category or tag) URL, whether you are already loading taxonomy.php, category.php, tag.php etc. theme template files:

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

For the current author archive, if the format author.php is used:

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

There is a feature for the homepage URL that returns the same URL on all pages, not just in the template front-page.php or home.php:

$current_url = home_url( '/' );

Video tutorial

Sum Up

That’s it, we hope you learned everything about WordPress Get Current URL Function. And for the visitor who asked us to write an article on How to fix “the link you followed has expired” WordPress, we have a dedicated article on this.

If you have any suggestions or queries, do comment and help us improve more.

Leave a Comment