Get the Current Page or Post URL in WordPress

One of the easiest way to get the current page or post url is to use the WordPress’s default the_permalink function. However, the_permalink function works well within the wordpress loop. In case if you want get the current page or post url outside of the loop, you can easily use the following code.

<?php 
echo "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>

This could be very useful to add values on meta tag like Open Graph Protocol.

Bare in mind though, I am using “https://” protocol instead of “http://” for demonstration purpose only. If your site does not have any SSL certificate assigned, go with the “http://”. For sites with SSL certificate “https://” would be highly recommended. In any case, please feel free to make the necessary adjustment as you desire. Just know that using wrong protocol may cause issues.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.