I am trying to set the page title, meta description and meta keywords to improve SEO on a ChronoForms v4 page by adding a Custom Code action in the On Load section:
<?php
if ($form->data['LongTitle']) {
$document = JFactory::getDocument();
$document->setTitle($form->data['LongTitle']);
}
if ($form->data['MetaDescription']) {
$document = JFactory::getDocument();
$document->setDescription($form->data['MetaDescription']);
}
if ($form->data['MetaKeywords']) {
$document = JFactory::getDocument();
$document->setMetaData('keywords', $form->data['MetaKeywords']);
}
?>
setDescription
and setMetaData
work fine but setTitle
does not work.
As per https://stackoverflow.com/a/31458607/1983389, it appears the article or menu option may set the page title later than the ChronoForm especially when loading the ChronoForm via the ChronoForms content plugin (as is the case in this example).
Tried adding a Meta Tager (sic) action in the On Load Section
The Meta Tager option has the same issue where the Meta Description and Meta Keywords are set OK but not the Page Title.
Tried changing the order of the content plugins
I tried moving the ChronoForms content plugin last but this didn't help.
Tried using Regular Labs Sourcerer to add code into the article
I tried adding the code directly into the article but this didn't work:
{chronoforms}Tour{/chronoforms}
{source}
<?php
$tour = JRequest::getVar('Tour');
$db = JFactory::getDBO();
$db->setQuery("SELECT cf_id,LongTitle FROM #__chronoforms_data_Tours WHERE cf_id='$tour'");
$result = $db->loadObject();
$tourname = $result->LongTitle;
if ($tourname) {
$document = JFactory::getDocument();
$document->setTitle($tourname);
}
?>
{/source}
loadResult()
seems like a better way to pull a single value from the row.