1

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}
1

JavaScript to the rescue

The Page Title can be set via JavaScript as per https://stackoverflow.com/a/24881134

In ChronoForms, add a "Load JS" action to the "On Load" section with the following code:

window.onload=function() {
  changeTitle ('<?php echo $form->data['LongTitle']; ?>')
}
function changeTitle (newTitle) {
  document.title=newTitle;            
}  

This works and apparently Google does crawl content injected by JavaScript.

Other search engines may not see JavaScript injected content so this is probably not the best practice and this method is only recommended where there is no better way to update the Page Title.

I'd be delighted to see some better answers than this poor work around.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.