How to Get the Current Page URL in WordPress

If you have ever tinkered with code in WordPress themes and plugins, you’ll likely come across situations where you need get the current page URL. This knowledge can come in especially handy when you want to use the URL in certain templates that are outside of the Loop. Here are several ways to get the current page URL in WordPress. Which one you pick depends on the context.

Method 1: The Catch-All

The following snippet works in any PHP template:

/* "" */

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

If you want just the slug, you can use this snippet:

/* "this-post" instead of "" */

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

Method 2: WordPress functions

WordPress has some built-in functions that help you get the current page URL. The function most often used is get_permalink(). If the current template file is single.php (or a custom post type variation such as single-project.php) or page.php, you can simply use get_permalink().

$current_url = get_permalink();

If the current template is for a taxonomy, such as taxonomy.php, category.php, or tag.php, use the following snippet:

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

For an author page such as author.php use:

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

Method 3: PHP Super Globals

Another way to get the current page URL in WordPress is by using PHP super globals. The $_SERVER super global contains information about the current request, including the current page URL. You can use the $_SERVER['REQUEST_URI'] property to get the current page URL:

$current_url = home_url( $_SERVER['REQUEST_URI'] );