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.

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.


Commenting is disabled.