If you are an WordPress or PHP Developer, I bet you know that once in a while you need to get the current URL of a dynamic page. Especially, if you are working on to add social sharing buttons or icons, it might be something that you need quite often.
Last night, when I was working on the Author template(author.php) file to add Facebook Like button for each author’s page. Very normally, I couldn’t make it work using normal the_permalink() function. I totally forgot that it’s a page not a post.
Basically, on WordPress we can use <?php the_permalink(); ?> function, to get the current post URL. But let’s not forget this function works only within the loop. If you place this function outside of the loop or any page of your WordPress site, you might get some irrelevant link URL which is something that you are not looking for.
Here is a little piece of code that can solve your problem like magic.
This code works both inside and outside of the loop. You can even use this code on any WordPress page as well. Hope that helps.
You can follow us on Twitter, add on your Google+ circle or Like our Facebook page to get the latest news, updates and reviews.








Comments: