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 Exchangeget_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 ) );
You may use the below code to get the whole current URL in WordPress:
global
current_url = home_url(add_query_arg(array(), $wp->request));
This will show the full path, including query parameters.
php - How to get current page URL in Wordpress 6.0? - Stack Overflow
php - Returning current URL in WordPress - Stack Overflow
How do Wordpress URL params work?
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.comIn case anyone else needs this. Below code should get the exact URL including parameters.
home_url($_SERVER['REQUEST_URI']);
You can echo or return it depending on your use case. e.g.
echo home_url($_SERVER['REQUEST_URI']);
or
return home_url($_SERVER['REQUEST_URI']);
The site_url() function returns the actual website root URL. Depending on where you are calling your URL you could try get_the_permalink() but a more surefire way would be to use the $wp->request method. Like this:
global $wp;
echo home_url( $wp->request )
The main problem with this function is that the URL parameters are left out, so if your link is something like: http://example.com/test/?myparam=1 it will only return http://example.com/test/