get_permalink() is only really useful for single pages and posts, and only works inside the loop.

The simplest way I've seen is this:

global $wp;
echo home_url( $wp->request )

$wp->request includes the path part of the URL, eg. /path/to/page and home_url() outputs the URL in Settings > General, but you can append a path to it, so we're appending the request path to the home URL in this code.

Note that this probably won't work with Permalinks set to Plain, and will leave off query strings (the ?foo=bar part of the URL).

To get the URL when permalinks are set to plain you can use $wp->query_vars instead, by passing it to add_query_arg():

global $wp;
echo add_query_arg( $wp->query_vars, home_url() );

And you could combine these two methods to get the current URL, including the query string, regardless of permalink settings:

global $wp;
echo add_query_arg( $wp->query_vars, home_url( $wp->request ) );
Answer from Jacob Peattie on Stack Exchange
🌐
SmartWP
smartwp.com › home › blog › how to get the current page url in wordpress (php)
WordPress: Get the Current Page URL in PHP (6 Ways + URL Parameters)
September 4, 2026 - It respects your permalink structure, ... anything. function my_get_current_url() { global $wp; return home_url( add_query_arg( array(), $wp->request ) ); } // Usage echo esc_url( my_get_current_url() );...
Discussions

php - How to get current page URL in Wordpress 6.0? - Stack Overflow
Trying to obtain the page url with the following code prior to Wordpress 6.0 works, but after update to Wordpress 6.0, $wp->request is coming through as an empty string on all pages. I have Pos... More on stackoverflow.com
🌐 stackoverflow.com
php - Returning current URL in WordPress - Stack Overflow
I am trying to implement canonical and hreflang tags in WordPress, but I cannot retrieve the current URL of the visited page. ... Save this answer. ... Show activity on this post. In case anyone else needs this. Below code should get the exact URL including parameters. More on stackoverflow.com
🌐 stackoverflow.com
How do Wordpress URL params work?
You might like checking out this tutorial on creating dynamic routes in WP: https://youtu.be/Wb5yEs5kV5k He covers how routing works and how you can override it. WPCasts is a goldmine if you're learning the mechanics behind WP. EDIT: You might need to learn a little bit about regex to get the most out of the above video: https://www.youtube.com/watch?v=rhzKDrUiJVk More on reddit.com
🌐 r/Wordpress
19
8
January 13, 2022
Site Upload Directory URL

https://developer.wordpress.org/reference/functions/wp_upload_dir/

Did you at least tried to Google "wordpress upload dir"? It's the first entry.

More on reddit.com
🌐 r/Wordpress
4
1
April 13, 2019
🌐
WP-Mix
wp-mix.com › wordpress-get-current-url
WordPress: Get Current URL and Slug | WP-Mix
May 2, 2023 - To get the current URL of any WP-generated web page (e.g., single post, archive, etc.), add the following code to theme functions file or plugin: global $wp; $current_url = home_url(add_query_arg(array(), $wp->request));
🌐
Meks
mekshq.com › home › blog › get current page url in wordpress
Get current page URL in WordPress (with a single line of code)
April 26, 2020 - For current author archive, if author.php template is used: For homepage URL, there is a function which returns the same URL on all pages, not only in front-page.php or home.php template: That’s it, feel free to try! Facebook Twitter LinkedIn Pinterest Reddit Viber WhatsApp 85Shares ... Co-founder and WordPress developer at Meks.
🌐
Transitory
transitory.technology › current-url-in-wordpress
Get the current URL in WordPress | Matthew Spencer
The idea is to use add_query_arg() with a bogus / empty query variable. This works because add_query_arg() has a fallback when no URI is passed. The falsey value removes the query variable from the path (this is how remove_query_arg() works). // This will return the current path including ...
Find elsewhere
🌐
Ultimate Blocks
ultimateblocks.com › how-to-get-the-current-page-url-in-wordpress
How to Get the Current Page URL in WordPress
November 19, 2024 - /* 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 ); You can use this function by adding the following code to ...
🌐
PluginsForWP
pluginsforwp.com › home › blog › how to get url of current page in wordpress
How to Get URL of Current Page in WordPress - PluginsForWP
November 1, 2023 - You can also use the add_query_arg function to add multiple queries to the URL, which can be very useful for search and filter components. When working with WordPress templates, you can use the specific code snippets below to utilize the template and get the current URL of the page.
🌐
WP Talky
wptalky.com › wordpress › how to get current page url in wordpress
How to Get Current Page URL in WordPress - WP Talky
August 29, 2022 - Copy and paste the code given below ... the WordPress PHP code which you can use to get the current page URL: global $wp; echo home_url( $wp->request );...
🌐
WP-Mix
wp-mix.com › get-current-url-wordpress
Get the Current URL in WordPress | WP-Mix
Quick tip: get the current URL in WordPress: global $wp; $current_url = add_query_arg($wp->query_string, '', home_url($wp->request));...
🌐
WordPress
developer.wordpress.org › reference › functions › get_site_url
get_site_url() – Function - WordPress Developer Resources
Retrieves the URL for a given site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
🌐
Qaisar Satti's Blogs
blog.qaisarsatti.com › home › wordpress get current url
Wordpress get current URL - Qaisar Satti's Blogs
July 8, 2024 - Today we talk about WordPress get the current URL. In this tutorial, We focus on getting the current URL for example you need to match or check what is current URL of the page Url or post URL or any other URL. Let’s start with our example. $currentUrl = home_url($_SERVER['REQUEST_URI']);
🌐
Konstantin Kovshenin
konstantin.blog › home › how to get the current url in wordpress
How to Get the Current URL in WordPress - Konstantin Kovshenin
February 24, 2012 - Here's a quick tip! I was wandering around the web for the perfect solution to retrieve the current URL in a WordPress theme or plugin. I found a bunch of solutions for PHP, but not directly related
🌐
Tech Crawlr
techcrawlr.com › how-to-get-url-of-current-page-or-post-in-wordpress
How To Get URL Of Current Page (or Post) In WordPress?
February 1, 2023 - The official advice given by ... = false) To get the URL of the current page without pagination, you can use the get_pagenum_link() function....
🌐
Insertcart
insertcart.com › get-current-page-url-in-wordpress
Get Current Page URL in WordPress – WordPress Theme Store – InsertCart
# relative current URI: <?php echo $current_rel_uri = add_query_arg( NULL, NULL ); ?> #absolute current URI: <?php echo $current_uri = home_url( add_query_arg( NULL, NULL ) ); ?> ... I’m a WordPress developer, Android app creator, and server management expert, handling almost every aspect of websites.
🌐
WPCare
wpcare.ai › blog › wordpress-get-current-url
wordpress get current url: 13 Expert Answers for Success
January 15, 2025 - Furthermore, there are several functions and methods within WordPress to retrieve the current URL, which we will explore throughout this article. The method to get current URL in WordPress varies depending on the context, such as whether you’re inside a template file, a plugin, or even an AJAX request.
🌐
GitHub
gist.github.com › leereamsnyder › fac3b9ccb6b99ab14f36
In WordPress, get current URL including query string · GitHub
September 8, 2014 - In WordPress, get current URL including query string - wp_get_current_page_url.php