It doesn’t matter if you need the URL for single post, page url, home page, category template, tag template, custom post type or any other WordPress template.

You can use this piece of code inside any PHP template file on your WordPress website:

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

And if you want WordPress to get current page slug only (last part without your base URL), here is the code:

/* For example if your website is "https://mekshq.com/some-post", 
it will return "some-post" */
 
global $wp;
$current_slug = add_query_arg( array(), $wp->request );

Bonus: Get current URL in WordPress on specific PHP templates
As an alternative way (not all-in-one solution), WordPress has its own functions which you can utilize depending on a page which is currently being displayed. Let’s take a look at the examples below.

For a single post or a page url, if single.php or page.php theme template file is currently loaded:

$obj_id = get_queried_object_id();
$current_url = get_permalink( $obj_id );

For current taxonomy term (eg. category or tag) url, if taxonomy.php, category.php, tag.php etc… theme template file is currently loaded:

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

For current author archive, if author.php template is used:

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

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:

$current_url = home_url( '/' );

That’s it, feel free to try!