How to get Current Page URL WordPress ( 3 Easy Ways )

As a web developer or WordPress site administrator, At some point, you may need to get the current page URL in the WordPress website. This is useful for generating links and knowing where your visitors are currently on your website.

Also, it is very helpful in debugging 404 error pages. So whatever the reason is, but you are here looking for a way on how to get current page URL in WordPress. Well, you have then visited the right place because in this guide, I will be sharing the code snippets to get current page URL in WordPress.

1) Get Current Page URL with Custom Code

To get the current page URL, you need the add the below code in your theme files or where ever you want to get the current page URL

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

With the above code, you can get the current URL of any posts, pages, custom post types, categories template, tags template, or any other templates in WordPress. However, if you want to display the current page URL on the front-end of the website then add this code in your code files:

    <?php
    global $wp;
    $current_url = home_url( add_query_arg( array(), $wp->request ) );
    echo $current_url;
     ?>

If you want to the get current page slug in your WordPress website, then use this code instead:

<?php
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
echo $current_slug; // Display the slug of page
?>

What is WordPress URL Slug

When you give your posts, pages, tags, categories, etc a title, WordPress automatically generates a slug for that page/post, etc, on the name of the title you have given. A slug is basically the last part of your page URL. For example:

If your Page URL is this: ” http://becures.com/about ” then the slug will be “about”

2) Get Current Page URL with WordPress Built-in Function

You can use the above custom PHP code to get the current page URL in WordPress but there is also a built-in function in WordPress to get the current page URL. So it’s up to you to use whether the Custom code or the built-in WordPress function, but both will smoothly work for you.

So to get the current page URL with WordPress built-in function then use this code:

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

You May Like:

How to get Featured Image URL in WordPress ( 2 Easy Ways )

How to Get Image URL in WordPress

How to Link External PHP File to HTML ( 2 Easy Methods )

How to Get Product ID in WooCommerce

As we know that WordPress has specific PHP templates like single.php, page.php, taxonomy.php, category.php, and etc. So if you want to get URLs on different PHP templates then I will also share the code for different PHP templates.

For Single.php and Page.php

To get the URL on Single.php or Page.php, add the below code on these PHP templates:

<?php
$obj_id = get_queried_object_id();
$current_url = get_permalink( $obj_id );
echo $current_url;
?>

Note: The Single.php refers to all posts on your WordPress site while Page.php refers to all pages on your website.

For taxonomy.php, category.php, tag.php etc

To get the current URL on taxonomy.php, category.php, tag.php, etc, add this below code in these PHP templates:

<?php
$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );
echo $current_url;
?>

For Aurthor.php

To get the current URL on Author.php, add the below code in Author.php WordPress template.

<?php
$obj_id = get_queried_object_id();
$current_url = get_author_posts_url( $obj_id );
echo $current_url;
?>

For Front-page.php or Home.php

To get the current URL on front-page.php or home.php, add the below code in these PHP WordPress templates.

<?php
$current_url = home_url( '/' );
echo $current_url;
?>

So with the help of these codes, you can easily get the current page URL on different PHP template files.

3) Using the get_permalink() Function:

This is the simplest and most straightforward method to fetch the current page URL in WordPress is by using the get_permalink() function. This function retrieves the permanent link (URL) for any post or page. Here’s how you can use it:

<?php $current_url = get_permalink(); echo $current_url; ?>

FAQ( How to get Current Page URL WordPress )

How do I get the current page URL in WordPress?

In order to get the current page URL on your website add the below code in your WordPress theme template files or where ever you want to add it:

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

How do I get the current post name in WordPress?

To get the current post or page name in WordPress, then you can use the below code:

<?php
$pagename = $post->post_name;
echo $pagename;
?>

How do I get the current page slug in WordPress?

To get the current page slug in your WordPress website, then use the below code:

<?php
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
echo $current_slug; // Display the slug of page
?>

Wrapping It Up

In this guide, I showed three easy ways to get the current page URL in your WordPress Website. The first one is by custom PHP code, the second way is by WordPress built-in function while the third is most easiest method. Since there are different types of PHP templates like single.php, page.php, category.php, and many more, so I also showed you how to get the current page URL on these PHP templates too.

I hope you find this article helpful and you get the desired information for which you were looking. If still your issue is not resolved with the article then you can contact me through the contact form or just leave a comment, I will be glad to assist you.

Also, If there are any particular topics in WordPress that you want me to write about, then please let me know in the comments below and I will surely help you out with that. Thanks for reading!

Hamza Afridi is a Full stack Web & WordPress developer and writer with 5+ years of experience. He is Founder of webtalkhub.com, a blog on web development, SEO, and digital marketing tutorials. He enjoys learning and sharing new technologies.

Leave a Comment