Kamran Ahmed
Kamran Ahmed

Reputation: 12438

Getting the permalink to the post in wordpress

I am trying to append the facebook like button to each of the post on my blog. I have managed to get whatever I need to add the like button, the only thing I need is, how can I access the current post's link inside the function author_bio_display($content) i.e. at the place where it says rawurlencode('post permalink goes here')?

function author_bio_display($content)  
{  
        $bio_box = '<iframe src="http://www.facebook.com/plugins/like.php?href='. rawurlencode('post permalink goes here') .'&amp;layout=standard&amp;show-faces=true&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';

        return $content . $bio_box;  
}  

add_action("the_content", "author_bio_display"); 

Upvotes: 0

Views: 5220

Answers (4)

Tahir Yasin
Tahir Yasin

Reputation: 11699

First thing is that the_content is not an Action Hook, its a Filter Hook so you should use add_filter instead of add_action.

function attach_like_button($content) {
  $post_id = $GLOBALS['post']->ID;
  $permalink = get_permalink($post_id);
  $link_button = ''; // Get latest facebook like button code from  https://developers.facebook.com/docs/reference/plugins/like/
  return $content.$link_button;
}

add_filter( 'the_content', 'attach_like_button' );

Upvotes: 1

Shashank Shah
Shashank Shah

Reputation: 2167

You need to do..

  $bio_box_id = get_permalink($post->ID);
  $bio_box = '<iframe src="http://www.facebook.com/plugins/like.php?href='. rawurlencode('*post permalink*') .'&amp;layout=standard&amp;show-faces=true&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';
  return $content . $bio_box;  

Upvotes: 0

Ben Racicot
Ben Racicot

Reputation: 5905

To get the current ID without making a global $post variable:

$id = get_the_id();

And

get_permalink($id);

Most out-of-loop functions begin with "get_" these functions do not echo but return data instead.

Upvotes: 2

Moeed Farooqui
Moeed Farooqui

Reputation: 3622

If you are currently on a detail page i.e single.php here I have defined a variable $post and save current POST->ID in $permalink.Now you can play with it.

function author_bio_display($content)  
{  

        global $post;
        $permalink = get_permalink($post->ID);
        $bio_box = '<iframe src="http://www.facebook.com/plugins/like.php?href='. rawurlencode('*post permalink*') .'&amp;layout=standard&amp;show-faces=true&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';

        return $content . $bio_box;  
} 

Upvotes: 0

Related Questions