Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Wordpress - How to ...
 
Share:
Notifications
Clear all

Wordpress - How to get URL of current page displayed?

1 Posts
1 Users
1 Likes
214 Views
 Hans
(@hans)
Famed Member Admin
Joined: 9 years ago
Posts: 2315
Topic starter  

I've found this one surprisingly unreliable when going through all the available methods.

I'll start with the one that worked reliably for my website:

$current_url = home_url($_SERVER['REQUEST_URI']);

Now some will complain about using the "REQUEST_URI" option, so here some alternatives that proved to be not always reliable, depending on your circumstances.

// Option 1
// requires $wp, may not work with permalinks set to plain
global $wp; 
$current_url = home_url( $wp->request );

// Option 2
// requires $wp, may not work with permalinks set to plain
global $wp;
$current_url = add_query_arg( $wp->query_vars, home_url( $wp->request ) );

// Some other options that may be helpful:
$current_url = get_permalink( get_the_ID() ); // for page
$current_url = get_category_link( get_query_var( 'cat' ) ); // for category

 


   
kedihas445 reacted
ReplyQuote
Topic Tags

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: