Facebook having trouble generating preview when sharing

Skinning and designing Serendipity (CSS, HTML, Smarty)
Post Reply
perlnerd
Regular
Posts: 37
Joined: Mon Mar 27, 2006 11:02 pm

Facebook having trouble generating preview when sharing

Post by perlnerd »

I have noticed that facebook is having issues with finding relevant text when sharing a blog post manually or via AddThis. The easiest solution seems to be to add a <meta description=... tag in the head of the page. Seems easier said than done though.

I'm using $head_title to set the <meta title=.. tag in the head of the page. Is there another variable available to index.tpl containing the entry body so I can truncate it and insert the meta tag in the head of the page?

If not, any other ideas on how to accomplish this?

Thanks
Clint
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Facebook having trouble generating preview when sharing

Post by Don Chambers »

Have you looked at the html meta tag plugin? See also this blog entry.
=Don=
perlnerd
Regular
Posts: 37
Joined: Mon Mar 27, 2006 11:02 pm

Re: Facebook having trouble generating preview when sharing

Post by perlnerd »

No, somehow I missed that one.

But, it doesn't work. The is due to the way I build the header, and not the fault of the plugin.

I have a header template file from our main website which has placeholders in it for things like the title, some banners and additional CSS. I have a function in index.tpl called DisplayHeader that grabs that template and inserts the relevent info into those placeholders. I pass necessary info from index.tpl to the function and have several bits like this:

Code: Select all

$css = <<<EOF
        <meta name="title" content="{$params['entrytitle']}" />
        <link rel="stylesheet" type="text/css" href="{$params['stylesheet']}" />
    <link rel="alternate"  type="application/rss+xml" title="{$params['title']} RSS feed" href="{$params['baseurl']}{$params['rwprefix']}feeds/index.rss2" />
    <link rel="alternate"  type="application/x.atom+xml"  title="{$blogTitle} Atom feed"  href="{$baseurl}{$params['rwprefix']}feeds/atom.xml" />
<script type="text/javascript" src="http://www.kingsnake.com/blog/plugin/ls-js"></script>
EOF;
then I replace the css placeholder in my header template file with $css

I did edit serendipity_event_metadesc.php and see if something like:

Code: Select all

$serendipity['smarty']->assign('metadescription','<meta name="description" content="' . htmlspecialchars($meta_description) . '" />' ."\n");
would allow me to access $metadescription in index.tpl and pass it to my DisplayHeader function as $params['metadescription']. Didn't seem to work.

If you're at all inclined to assist me, it would be much appreciated.

Thanks
Clint
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Facebook having trouble generating preview when sharing

Post by garvinhicking »

Hi!

Your DisplayHeader function could instead access $smarty->get('metadescription') - when the variable is assigned, you can use it. Otherwise you would need to pass {$metadescription} to your {DisplayHeader} smarty function call to have it available as a $param.

HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Facebook having trouble generating preview when sharing

Post by Don Chambers »

IMHO I still feel the meta tag plugin is ideal for this... not only does it solve your facebook issue, but allows you to provide individual meta descriptions for each entry that are not just a truncated version of your entry body.
=Don=
perlnerd
Regular
Posts: 37
Joined: Mon Mar 27, 2006 11:02 pm

Re: Facebook having trouble generating preview when sharing

Post by perlnerd »

Don: Yes, your plugin basically does what I need. I'll just need to do some minor modifications to get the data it generates in a format I can use.

Thanks for the help Garvin and Don.

Clint
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Facebook having trouble generating preview when sharing

Post by Don Chambers »

perlnerd wrote:Don: Yes, your plugin basically does what I need. I'll just need to do some minor modifications to get the data it generates in a format I can use.

Thanks for the help Garvin and Don.

Clint
For the record, its not "my" plugin.... I simply made a minor modification to it a few years ago. :wink: Hope everything works out for you!
=Don=
perlnerd
Regular
Posts: 37
Joined: Mon Mar 27, 2006 11:02 pm

Re: Facebook having trouble generating preview when sharing

Post by perlnerd »

My solution won't apply to too many people but just incase:

I split my DisplayHeader function into two functions. One to process and display everything in my remote header file up to my {css} placeholder and one to display everything after my {css} placeholder.

This lets me insert {serendipity_hookPlugin hook="frontend_header"} and whatever else I need between these functions. This gets the live search .js and meta description in the right place.

I also made a new copy of the meta tag plugin and modified extract_description() to truncate the description text on a word boundary.

Code: Select all

$text = strip_tags($text);
        $text = substr($text,0,250);
        $i = strrpos($text," ");
        $title = substr($text,0,$i) . "...";
Post Reply