Jeff VIP
Total posts: 745
06 Nov 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!

Last Modified: 12 Jul 2016


clowride VIP
Total posts: 1,022
06 Nov 2013 05:59

thank you very much


Sergey
Total posts: 13,748
07 Nov 2013 07:06

I think fo purpose like this it is reasonable to create field template.


Jeff VIP
Total posts: 745
07 Nov 2013 07:24

Sergey

I think fo purpose like this it is reasonable to create field template.

Any volunteers?


pepperstreet VIP
Total posts: 3,837
07 Nov 2013 08:49

Is there ANY good real-world example, that shows the actual purpose and results? My visual designer-brain hates those parameter-deserts! :S ;-)


Jeff VIP
Total posts: 745
07 Nov 2013 09:25

@pepperstreet

Ha, ha, I know excactly what you mean :)

Some purposes:

Viralize your articles on Facebook

[

AddThis uses Open Graph tags for their Smart Layers]( https://www.addthis.com/get/smart-layers#.UnuwHOI4mYk )


pepperstreet VIP
Total posts: 3,837
06 Jan 2014 05:26

FYI - ITP Meta by itprism.com

Pro Version supports Cobalt ;-)


Guest
12 Feb 2014 08:29

Guest
12 Feb 2014 13:26

see the forum post (follow the link in my previous comment)

he says if he found it's a bug, than he's going to fix it in next release (next week)


Guest
27 Feb 2014 06:53

UPDATE:

ITP meta (pro version) is still not working for cobalt, plus it has other bugs to be fixed

the developer refunded me for now as he said he has no time to fix it quickly:

anyone interested in this plugin should write him here http://itprism.com/contact

and tell him that he's intersted in the pro version for cobalt, this shoul help to have the plugin fixed more quickly

thank you


techutopia
Total posts: 202
08 Oct 2014 01:51

This is excellent.

Could anyone give me any pointers as to how I would get a cobalt text field for a record and make it as a og:description?

Thankyou.

Dale.


clowride VIP
Total posts: 1,022
08 Oct 2014 06:40

i use itp meta and it perfectly works


Sergey
Total posts: 13,748
09 Oct 2014 10:52

Why not simply create custom image field output template?


klox7 VIP
Total posts: 914
31 Mar 2015 17:06

techutopia Could anyone give me any pointers as to how I would get a cobalt text field for a record and make it as a og:description?

So did anybody try to use textarea field intro text as og description? How did you achieve it?


pepperstreet VIP
Total posts: 3,837
31 Mar 2015 21:58

What about the field-based solution by Andi (aka JoomBoost) ;)


  • You might also follow the initial example made by Jeff. Add the og:description tag line and populate it with the Cobalt textarea field by ID.

nican VIP
Total posts: 392
28 Oct 2015 09:00

I tried you solution Jeff, but I see this error message in frontend

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseEntityRef: expecting ';' in Entity, line: 1 in /home/xxx/public_html/components/com_cobalt/views/record/tmpl/default_record_blog-custom.php on line 97

the og:tags are being created correctly though

line 97 is

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


nican VIP
Total posts: 392
28 Oct 2015 12:18

Thanks Sergey, still can't say why is happening, I have used the same solution in other sites some time ago and it was working.

I have bought Andi (aka JoomBoost) custom field, but it seems to not work if you have a gallery field instead that an image field and you want to use it for the og:image open graph

let's see... more testing to do


Jeff VIP
Total posts: 745
29 Oct 2015 00:32

nican I tried you solution Jeff, but I see this error message in frontend

I see. This is because you have used a gallery field and not an image field. We need to get the url of the gallery thumbnail.

@Sergey: I tried to fetch the gallery thumbnail, but to no avail. ( http://www.mintjoomla.com/support/community-forum/user-item/76-sackgesicht/48-cobalt-8/1719-gallery-thumbnail-generation.html )

Any suggestions?


Jeff VIP
Total posts: 745
29 Oct 2015 01:28

Interestingly the code does fetch a gallery thumbnail. The last thumbnail that is. ;-|

Only when you set your gallery template to full.php it will throw warnings, but you can hide them by adding

libxml_use_internal_errors(true);

I have added this line in the example.


Jeff VIP
Total posts: 745
29 Oct 2015 01:54

techutopia Could anyone give me any pointers as to how I would get a cobalt text field for a record and make it as a og:description?

I have updated the example code :-)

Powered by Cobalt