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:

  if ($form->data['LongTitle']) {
    $document = JFactory::getDocument();
  if ($form->data['MetaDescription']) {
    $document = JFactory::getDocument();
  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:

    $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();

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) {

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 agree to our terms of service, privacy policy and cookie policy

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