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

[Solved] Wordpress - How to get URL of current page displayed?

1 Posts
1 Users
1 Reactions
958 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
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
Share: