Getting the thumbnails to appear in the facebook sharer from a SilverStripe content field

Posted by Gav on 9 August 2011 | 0 Comments

Tags: ,

I recently worked on a project that uses SilverStripe and uses the facebook sharer to share content and I noticed that the images from the $Content field are not converted into thumbnails. This is my solution for the problem.

When the sharer scapes your page, it does not parse images such asĀ 

<img src="assets/Uploads/_resampled/resizedimage400579-image006.jpg">

which is what SilverStripe renders a content field as.

The solution is to add the following code to your mysite/code/Page.php file.

class Page_Controller extends ContentController {

	...

	public function init() {
		parent::init();
		
		$this->Content = str_replace('src="assets/', sprintf('src="%sassets/', $this->baseHref()), $this->Content);


	}

	...
}

What the code does is it adds the BaseHref to the front of the images. This allows the facebook sharer to scrape them.


Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments