Jeff VIP
Total posts: 745
06 Нояб 2013 05:49

Hi,

if you need to add Open Graph tags to your Cobalt records, just add somewhere after line 21 of components/com_cobalt/views/record/tmpl/default_record_article.php

<?php // OPEN GRAPH TAGS
//libxml_use_internal_errors(true); //Uncomment this line if you use a gallery field as record image.
//Get description text   
if (isset($item->fields_by_id[6])): //Replace 6 with your description field ID
    $descraw = substr($item->fields_by_id[6]->value, 0, 200).'...'; //Truncate text to 200 characters
    $desc = strip_tags($descraw); //Strip text from xhtml markup
endif;

// Get thumbnail url
$dom = new DOMDocument;
$dom->loadHTML($item->fields_by_id[12]->result);// Replace 12 with your image/gallery ID
$x = new DOMXPath($dom); 
foreach($x->query("//img") as $node) 

// insert tags
$opengraph  = '<meta property="og:title" content="'.$item->title.'"/>' ."\n";
$opengraph .= '<meta property="og:type" content="article"/>' ."\n";
$opengraph .= '<meta property="og:description" content="'.$desc.'"/>' ."\n";
$opengraph .= '<meta property="og:url" content=" http://mysite.com '.JRoute::_($item->url).'"/>' ."\n";
$opengraph .= '<meta property="og:image" content=" http://mysite.com '.$node->getAttribute("src").'"/>' ."\n";
$opengraph .= '<meta property="og:site_name" content="'."MY WEBSITE NAME".'"/>' ."\n";
$opengraph .= '<meta property="fb:admins" content="'."MY FACE BOOK DEVELOPER CODE".'"/>' ."\n"; // Not sure if you need a developer code. If not, comment this line out

$doc = JFactory::getDocument();
$doc->addCustomTag($opengraph);
?>

@Edit: Removed some redundant code and lines, added strip_tags to filter html.

Read also:

http://ogp.me/

Have fun!

Последние изменения: 12 Июль 2016


Sergey
Total posts: 13,748
29 Окт 2015 05:55

I am not familiar with xpath. I do not know why it does not extracts all images.


nican VIP
Total posts: 392
29 Окт 2015 09:42

Thanks Jeff, really really appreciated!


Jeff VIP
Total posts: 745
29 Окт 2015 23:02

Sergey I am not familiar with xpath. I do not know why it does not extracts all images.

http://www.the-art-of-web.com/php/html-xpath-query/

Back then I used DOMDocument and DOMXpath to fetch the source of an image field, because I didn't know how to retrieve just the path.

Now I think I can rewrite this code without using DOMDocument and DOMXpath if only I could find a universal solution to fetch ONE image url of an image OR gallery field.

Any idea how, Sergey?


Sergey
Total posts: 13,748
30 Окт 2015 03:44

Jeff if only I could find a universal solution to fetch ONE image url of an image OR gallery field.

Where in article full template? YOu can always access raw field data $item->fields_by_id[22]->value which wil be an array of images with al needed information.


Jeff VIP
Total posts: 745
30 Окт 2015 09:13

Sergey Where in article full template? YOu can always access raw field data $item-&gt;fields_by_id[22]-&gt;value which wil be an array of images with al needed information.

Ah ok. So if you want to use the first image of a gallery field:

<?php // Open Graph tags
//Get description text    
if (isset($item->fields_by_id[6])): //Replace 6 with your description field ID
    $desc = substr($item->fields_by_id[6]->value, 0, 200).'...'; // Truncate text to 200 characters
endif;

// Get thumbnail url
$thumb = null;
if (isset($item->fields_by_id[13])): //Replace 13 with your gallery field ID
    $thumbarray = $item->fields_by_id[13]->value;
    $thumb = $thumbarray[0]['fullpath'];    
endif;

// insert tags
$opengraph  = '<meta property="og:title" content="'.$item->title.'"/>' ."\n";
$opengraph .= '<meta property="og:type" content="'."article".'"/>' ."\n";
$opengraph .= '<meta property="og:description" content="'.$desc.'"/>' ."\n";
$opengraph .= '<meta property="og:url" content=" http://mysite.com '.JRoute::_($item->url).'"/>' ."\n";
$opengraph .= '<meta property="og:image" content=" http://mysite.com/uploads/gallery/ '.$thumb.'"/>' ."\n";
$opengraph .= '<meta property="og:site_name" content="'."MY WEBSITE NAME".'"/>' ."\n";
$opengraph .= '<meta property="fb:admins" content="'."MY FACE BOOK DEVELOPER CODE".'"/>' ."\n"; // Not sure if you need a developer code. If not, comment this line out

$doc = JFactory::getDocument();
$doc->addCustomTag($opengraph);
?>

nican VIP
Total posts: 392
30 Окт 2015 11:11

I tried your updated code but I get this error from facebook debugger

Your page has meta tags in the body instead of the head. This may be because your HTML was malformed and they fell lower in the parse tree. Please fix this in order for the tags to be usable.

Code I used

<?php // Open Graph tags
libxml_use_internal_errors(true); //Uncomment this line if you use a gallery field as record image.

//Get description text   
if (isset($item->fields_by_id[6])): //Replace 6 with your description field ID
    $desc = substr($item->fields_by_id[6]->value, 0, 200).'...'; // Truncate text to 200 characters
endif;

// Get thumbnail url

$dom = new DOMDocument;

$dom->loadHTML($item->fields_by_id[67]->result);// Replace 12 with your image/gallery ID

$x = new DOMXPath($dom); 
foreach($x->query("//img") as $node)

// insert tags

$opengraph  = '<meta property="og:title" content="'.$item->title.'"/>' ."n";
$opengraph .= '<meta property="og:type" content="'."article".'"/>' ."n";
$opengraph .= '<meta property="og:description" content="'.$desc.'"/>' ."n";
$opengraph .= '<meta property="og:url" content=" http://www.alkemica.net '.JRoute::_($item->url).'"/>' ."n";
$opengraph .= '<meta property="og:image" content=" http://www.alkemica.net '.$node->getAttribute("src").'"/>' ."n";
$opengraph .= '<meta property="og:site_name" content="'."Alkemica.net".'"/>' ."n";
// $opengraph .= '<meta property="fb:admins" content="'."MY FACE BOOK DEVELOPER CODE".'"/>' ."n"; // Not sure if you need a developer code. If not, comment this line out

$doc = JFactory::getDocument();
$doc->addCustomTag($opengraph);

?>

pasted right after the tag in the template

when I look at the soucre code I can see that the metatags are before the tag, but still facebook throws that error maybe because the metatags contain some "n" signs between them, and some html code in the og:description tag? Couldn't say... im out of my habitat here

  <meta property="og:title" content="Gli amori e l’Amore"/>n<meta property="og:type" content="article"/>n<meta property="og:description" content="<p>Quante volte nella vita abbiamo avuto amori? Forse anche per un giorno o un mese, poi tutto passa, lasciando a volte un gradevole ricordo a volte meno.</p>
<p><br />Ma l’Amore, quello con la A m..."/>n<meta property="og:url" content=" http://www.alkemica.net/contenuti/voce/12-crescita-personale/352-gli-amori-e-l-amore "/>n<meta property="og:image" content=" http://www.alkemica.net/cache/cobalt_thumbs/gallery67-352/175/66ce4ed158bf9b6601e6b0779bb8cdd6.jpg "/>n<meta property="og:site_name" content="Alkemica.net"/>n

Also, I inserted the ID of an html field to use for the og:description... maybe this is what is causing these issues.


Jeff VIP
Total posts: 745
30 Окт 2015 12:27

Check my updated example above...

Somehow the new line modifiers "\n" turned into "n" when I updated my post. There should be one meta property per line and not all wrapped up like in your example.


nican VIP
Total posts: 392
31 Окт 2015 10:51

Oh I see...

Do you know what would make your script really great?

1 - Automatically delete html tags from the og:description 2 - set the url of a default image to fetch if there is no image in the shared record

:-)


Jeff VIP
Total posts: 745
31 Окт 2015 22:37

nican 1 - Automatically delete html tags from the og:description

Already done


nican VIP
Total posts: 392
02 Нояб 2015 16:02

Jeff Already done

mmm... I still see them

og_description_2


Jeff VIP
Total posts: 745
02 Нояб 2015 23:52

nican mmm... I still see them

Did you use this in your code?

//Get description text   
if (isset($item->fields_by_id[6])): //Replace 6 with your description field ID
    $descraw = substr($item->fields_by_id[6]->value, 0, 200).'...'; //Truncate text to 200 characters
    $desc = strip_tags($descraw); //Strip text from xhtml markup
endif;

nican VIP
Total posts: 392
03 Нояб 2015 09:04

Nope, this

$desc = strip_tags($descraw); //Strip text from xhtml markup

was missing, I must have copied the second code block you have pasted.

Thank you!


andiamine VIP
Total posts: 295
06 Нояб 2015 23:37

I have updated opengraph field, here list of new changes:

  • Strip bbcode in description field
  • use meta description of article if field is empty
  • use default picture if user didn't upload a picture
  • Auto detection of image field type
  • gallery field support

check changelog here: https://www.joomboost.com/support/changelogs/category-items/2-joomboost-changelogs/48-open-graph-field.html


Гость
05 Июль 2016 19:38

Hallo I try to put the code following: components/com_cobalt/views/record/tmpl/default_record_article.php but there is only components/com_cobalt/views/record/tmpl/default_record_news.php Btw I try to add code after line 21 but take a white page in my browser.


jimint
Total posts: 1
10 Июль 2016 11:24

Hallo I try to add the code in following: components/com_cobalt/views/record/tmpl/default_record_article.php but there is only components/com_cobalt/views/record/tmpl/default_record_news.php Btw I try to add code after line 21 but take a white page in my browser. Do you have any idea to find the solution because the image after sharing in the new its not the image itself.


Sergey
Total posts: 13,748
12 Июль 2016 04:59

jimint Hallo I try to add the code in following: components/com_cobalt/views/record/tmpl/default_record_article.php but there is only components/com_cobalt/views/record/tmpl/default_record_news.php

The name of the templates my differ. It depends what names did you you use to create templates.

jimint Btw I try to add code after line 21 but take a white page in my browser.

Please google for "Joomla white screen of death" and fix it first so you can see the error. Then when you see the error you might get an idea wjat to do next.

Работает на Cobalt