In most scenarios
get_permalink() will cover your needs, however, that will only get you the current page URL for single posts, pages, and custom post types and only works within the loop.
get_post_type_archive() for post type archive URLs and
get_term_link() for category/taxonomy URLs but it would be nice if we had one all-encompassing function that worked in all scenarios.
You may have seen long complicated functions using
$_SERVER['HTTPS'] to work out the protocol and joining this with
$_SERVER['HTTP_HOST'] before appending
$_SERVER['REQUEST_URI'] but this can all be simplified down to a one-liner. We let WordPress’ built-in
home_url() function do all the heavy lifting.
WordPress Get Current URL Function
You don’t need to worry about
$_SERVER['REQUEST_URI'] ever being empty as WordPress calls the
wp_fix_server_vars() function on every load to ensure this and other server variables it relies on exist. As a bonus, this preserves query string values ensuring it works with plain and custom permalink structures.
Remember to escape the output of the above function, as
$_SERVER['REQUEST_URI'] can be source of unsafe user input which could be used in XSS (Cross-Site Scripting) attacks. Don’t worry, this is a simple as passing the output to
esc_url() when used in HTML attributes and
esc_html() otherwise. You’re in the habit of doing this anyway though, aren’t you?