Keyword

Add an alternative title field to use for social media posts

  • Nadal Kumar
  • Nadal Kumar's Avatar Topic Author
  • Offline
  • Junior Member
More
6 years 5 months ago #165097 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.
  • 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
  • Nadal Kumar's Avatar Topic Author
  • Offline
  • Junior Member
More
6 years 5 months ago #165098 by Nadal Kumar
Correction
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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
6 years 5 months ago #165110 by Krikor Boghossian
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 :)

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum