retrieving entry body's first picture for open graph's og:im

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

retrieving entry body's first picture for open graph's og:im

Post by peggylon »

I'm back with another question and even with searching the forum for hours I seem not to be able to solve it on my own...

I'd like to gain controll over what is shared on social media, by implementing open graph tags.
I figured, there are two ways of doing it.

My first approach was editing entries.tpl like that:

Code: Select all

{if $view eq "entry" && isset($is_single_entry)}
   <meta property="og:title" content="{$entry.properties.meta_head_title|escape}" />
   <meta property="og:type" content="article" />
   <meta property="og:url" content="{$entry.rdf_ident}" />
   <meta property="og:description" content="{$entry.properties.meta_description}" />
   <meta property="og:image" content="{?}" />
{else}
   <meta property="og:title" content="{$blogTitle}" />
   <meta property="og:type" content="blog" />
   <meta property="og:url" content="{$serendipityBaseURL}" />
   <meta property="og:description" content="{$blogDescription}" />
   <meta property="og:image" content="url-of-my-standard-blog-picture" />
{/if}
<meta property="og:site_name" content="{$blogTitle}" />
Is it possible to retrieve an article's first picture from $entry.body without having to touch any php file? I try to avoid editing plugin or system files, since these changes tend to get lost with the next update.

Secondly I'm aware of the existence of the facebook plugin and tested it. Even though it implements the desired picture, it seems less useful than the first approach. Mainly because the plugin pulls text from the entry body. Not sure if and how one could merge the entry meta_description into the facebook plugin output.
Also it costs performance to read the API for comments, which I'm not particularly interested in. Finally I would still have to edit entries.tpl to set meaningful sharing options for pages, that are no single articles.

I wonder what will turn out as the solution of choice. With your help, hopefully. :)
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: retrieving entry body's first picture for open graph's o

Post by garvinhicking »

Hi!

Now as for retrieving the articles first picture, the best way would be to use regular expressions for that. I don't think you can actually do this within raw Smarty code (correct me if I'm wrong), so you would need to register a custom smarty modifier in your templates config.inc.php (via $serendipity['smarty']->register_modifier('...')). When you have your own template, this code would be fully distinct on its own and survive any upgrade (just like a .tpl file).

About the Facebook plugin; the meta descript should actually be available inside the $eventData for the plugin itself, so one could actually add an option to draw the description from that tag, instead of the other one. Also, one could add an option whether to check the comments or not. It's not really too hard PHP code and would be a great first step in modifying plugins (and we would of cours eimplement this to the official plugin), so if anyone (or even you yourself) feels motivated to try it out, please do. I'm here to help if there are issues/questions :-)

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/
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

[solved retrieving entry body's first picture for open graph

Post by peggylon »

Hi Garvin,

thanks for your help. I reckon, that the first approach is still my best bet. The plugin uses regular expressions for fetching the picture as well. That's what did the trick, just as you suggested...

in config.inc.php:

Code: Select all

function get_image($param) {
if (preg_match('@<img.*src=["\'](.+)["\']@imsU', $param, $im)) {
    return $im[1];
    }
}

$serendipity['smarty']->register_modifier('get_image', 'get_image');
in entries.tpl:

Code: Select all

<meta property="og:image" content="{$entry.body|get_image}" />
I also looked deeper into the plugin code to see, whether I'd possibly be capable of implementing the suggested changes. I figured out where and how the open graph tag writing would have to be edited (in an extended class). I didn't quite understand where the backend content gets written, though.

Not sure if I want to venture further in that direction... :roll:

Peggy
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
gtfreymann
Regular
Posts: 7
Joined: Mon Feb 28, 2011 4:06 pm

Re: retrieving entry body's first picture for open graph's o

Post by gtfreymann »

What did you end up doing?

We are looking for the same solution.

I noticed that putting the code snippet into entries.tpl adds the open graph tags in the wrong place (and some of the fields are empty).

I checked your blog and I see you have them in between the <head> tags now.

Can you share what you ended up doing to accomplish this?

Thanks!
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: retrieving entry body's first picture for open graph's o

Post by Timbalu »

Just to note:

With Serendipity 1.7 (Smarty 3 API changes) this line

Code: Select all

$serendipity['smarty']->register_modifier('get_image', 'get_image');
needs a re-write to

Code: Select all

$serendipity['smarty']->registerPlugin('modifier', 'get_image', 'get_image');
or, if that is a template to be used in both worlds, like this

Code: Select all

if( defined('Smarty::SMARTY_VERSION') ) {
    $serendipity['smarty']->registerPlugin('modifier', 'get_image', 'get_image');
} else {
    $serendipity['smarty']->register_modifier('get_image', 'get_image');
}
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
gtfreymann
Regular
Posts: 7
Joined: Mon Feb 28, 2011 4:06 pm

Re: retrieving entry body's first picture for open graph's o

Post by gtfreymann »

The facebook event plugin seems like an easy solution to adding open graph tags to your page.

I installed that and things are working fine.
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: retrieving entry body's first picture for open graph's o

Post by peggylon »

@gtfreymann Sorry I missed your earlier post... Thanks for pointing me to the facebook plugin!

@Timbalu Thanks for the hint with modifier calls in Smarty 3. I changed that.

The solution for open graph tags in the <head> section as discussed earlier had been working fine till recently. Not anymore, though. The $entry variable seems no longer available outside of entries.tpl.
One should be able to retrieve the current entries values from $entries instead. Seems tricky though to access $entries.key.entries values, since key is a timestamp...
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: retrieving entry body's first picture for open graph's o

Post by Timbalu »

peggylon wrote:The $entry variable seems no longer available outside of entries.tpl.
One should be able to retrieve the current entries values from $entries instead. Seems tricky though to access $entries.key.entries values, since key is a timestamp...
File entries.tpl should assign the entry var for 'out of "natural" scopes' like this:

Code: Select all

    {foreach from=$dategroup.entries item="entry"}
        {assign var="entry" value=$entry scope="parent"}
Which template are you using? Do you have that? (Should have been noted with the upgrade to 1.7-rc2 or above.)
If that still does not help, try with scope="root" and - if still impossible - with scope="global". Root should be the scope which could already help, since being the same scope as in old Smarty 2. With "global" it will be available globally, but you can never know, where it might badly interact with other $entry vars. By changing this value, you might need to purge the last compiled entries.tpl.php after each change to really see effects.
If that is still impossible, you have to use $entries and get the the right value by iterating.

Btw, when you disable (rename) the admin dir in your template, you will get the default admin dir in future, which is a tad newer.
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
peggylon
Regular
Posts: 51
Joined: Tue Jul 01, 2008 6:32 pm
Location: Berlin
Contact:

Re: retrieving entry body's first picture for open graph's o

Post by peggylon »

Code: Select all

{foreach from=$dategroup.entries item="entry"}
        {assign var="entry" value=$entry scope="parent"}
Which template are you using? Do you have that? (Should have been noted with the upgrade to 1.7-rc2 or above.)
Thanks Ian! That's the missing piece...
I had been using my adapted entries.tpl from before the s9y update. I added the assign var line with scope="root" and things are back to normal. You made my day!
----------
peggylon aka multikulinaria http://www.multikulinarisch.es
Post Reply