- Posts: 28
COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- Add an alternative title field to use for social media posts
Add an alternative title field to use for social media posts
- Nadal Kumar
- Topic Author
- Offline
- Junior Member
Less
More
7 years 3 weeks ago #165097
by Nadal Kumar
Add an alternative title field to use for social media posts was created by Nadal Kumar
The facebook gods have removed the ability to alter data scraped from pages, due to abusers who change the title to mislead viewers. While there were evil doers wrecking the system, there are good reasons why one would want the post title, text and even image to differ from the actual post at the website.
Add the following to the php head of your K2 template item.php
- Your actual webpage post title and image may be considered as 'NSFW' and offend some squeamish folks so you need to use a much more conservative, yet relative combination to serve to social media.
- Your page title may be quite long and it is being clipped so it doesn't make sense when shared an you may not get the clicks
Add the following to the php head of your K2 template item.php
// check if fields enable and in use for current item then create a variable for the fields of the assigned group
if( $this->item->params->get('itemExtraFields') && count($this->item->extra_fields) ) {
$fields = $this->item->extraFields;
}
$document = JFactory::getDocument();
// if social title is set, remove default title and write new title
if( isset($fields->socialtitle->value) ) {
// remove the existing og meta title
unset($document->_metaTags['name']['og:title']);
// set the og meta title
$document->setMetaData('og:title', $fields->socialtitle->value, 'property');
$document->setMetaData('twitter:title', $fields->socialtitle->value);
}
if( isset($fields->socialimage->value) ) {
unset($document->_metaTags['name']['og:image']);
// set the image url as written in the custom field or add the site url if only a relative url was entered
$socialimage = $fields->socialimage->value;
if( strstr($fields->socialimage->value, 'http') === false ) $socialimage = JUri::root().'images/'.$fields->socialimage->value;
$document->setMetaData('og:image', $socialimage, 'property');
$document->setMetaData('twitter:image', $socialimage);
}
Please Log in or Create an account to join the conversation.
- Nadal Kumar
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 28
7 years 3 weeks ago #165098
by Nadal Kumar
Replied by Nadal Kumar on topic Add an alternative title field to use for social media posts
Correction
The above code has an error but editing it was not possible. The entire code should be wrapped in extrafield check
The above code has an error but editing it was not possible. The entire code should be wrapped in extrafield check
// check if fields enable and in use for current item then create a variable for the fields of the assigned group
if( $this->item->params->get('itemExtraFields') && count($this->item->extra_fields) ) {
$fields = $this->item->extraFields;
$document = JFactory::getDocument();
// if social title is set, remove default title and write new title
if( isset($fields->socialtitle->value) ) {
// remove the existing og meta title
unset($document->_metaTags['name']['og:title']);
// set the og meta title
$document->setMetaData('og:title', $fields->socialtitle->value, 'property');
$document->setMetaData('twitter:title', $fields->socialtitle->value);
}
if( isset($fields->socialimage->value) ) {
unset($document->_metaTags['name']['og:image']);
// set the image url as written in the custom field or add the site url if only a relative url was entered
$socialimage = $fields->socialimage->value;
if( strstr($fields->socialimage->value, 'http') === false ) $socialimage = JUri::root().'images/'.$fields->socialimage->value;
$document->setMetaData('og:image', $socialimage, 'property');
$document->setMetaData('twitter:image', $socialimage);
}
}
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
7 years 2 weeks ago #165110
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Add an alternative title field to use for social media posts
That's nice Nadal,
this can be used in overrides so its 100% update proof :)
this can be used in overrides so its 100% update proof :)
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Forum
- K2 Community Forum
- English K2 Community
- Add an alternative title field to use for social media posts