Getting current page url and title for social network sharing in WordPress

Today i added a few social network sharing buttons i.e. facebook, twitter, delicious, myspace etc. to the right side column of this very blog.  To make it work as desired I had to do some extra bit of effort to create respective urls and titles for sharing. As there seems no particular function or variable in WordPress which could give me the exact page url of the page i am currently viewing i had to create a custom function (with the help of some kind guys around the web).

So all in all, I needed exactly two parameters 1. $url and 2. $title to post links on social networking sites so here’s how i prepared them with the help of a custom function and two wordpress functions together.

For example, for facebook i would need $url and $title as shown as shown in{$url}/&t={$title}&v=3

For the $url parameter i used the following custom function which gives me the current page url:
function CurrentPageURL()  {
$pageURL = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
return $pageURL;
$url = CurrentPageURL();

which gives me: if it was the Home Page if it was the HTML and CSS category page if i was the October 2010 archives page if i was a particular (single) post page
…so on.

And for the $title i used this:
$title = get_bloginfo() . wp_title(" | ", false);
Which gives me : if i was the Home Page, | HTML and CSS, if i was the HTML and CSS category page | 2010 | October, if i was October 2010 archives page | Center aligning a table within DIV element, if i was a particular (single) post page
and so on.

Please click get_bloginfo() and wp_title() to check how these particular WordPress functions work.

I hope this helps someone.

2 thoughts on “Getting current page url and title for social network sharing in WordPress

  1. @Dusan Vlahovic – Sorry if i was late to respond. I use “Executable PHP widget” ( to execute php code in the sidebar and put code with function directly there in the widget.

  2. Hi. Really useful article, but i have a couple of questions. That custom function would go on the functions.php file?, the title code also?.

    I couldn´t get it to work, and also, what´s the code you put on the sidebar?.

    Thanks a lot!.

Leave a Reply

Your email address will not be published. Required fields are marked *