onOutput

The onOutput handler is one of the most commonly used for simple customizations. In the front-end or the back-end, it passes the entire page contents through the $buffer variable, allowing you to edit text using PHP functions like str_replace and preg_replace.

Note that this is mostly useful only for LiveWhale CMS. While onOutput does run for the initial page HTML of LiveWhale Calendar pages, individual views are loaded via JS/AJAX and don’t get processed by this handler.

Note: onOuput is run on every page of your site, and regex operations like preg_replace can be surprisingly labor-intensive on your server. Make sure to use if/then to only run onOuput processes when they’re necessary.

<?php
    $_LW->REGISTERED_APPS['my_app']=[
       'title'=>'My App',
       'handlers'=>['onOutput'],
    ];

    class LiveWhaleApplicationMyApp {

      public function onOutput($buffer) { 
        global $_LW;

        // Do whatever you want to $buffer here...

        return $buffer;
      }

   }
?>

 

Examples

<?php
    $_LW->REGISTERED_APPS['my_app']=[
       'title'=>'My App',
       'handlers'=>['onOutput'],
    ];

    class LiveWhaleApplicationMyApp {

      public function onOutput($buffer) { 
        global $_LW;
        
        // Example: Add a custom message to the news editor page
        if ($_LW->page == 'news_edit') {
          $new_content='<div class="fields">
            My custom instruction text goes here.
          </div>';
          $buffer=preg_replace('~(<!– START TAGS –>)~s', $new_content.'\\1', $buffer);
        }

        // Example: Tweak text in the LiveWhale Dashboard
        if ($_LW->page==='dashboard') { // if this is the welcome page
          $buffer=str_replace('Need help?','Do you need help?',$buffer); // swap in an alternate help msg
        };

        // Change a certain bit of code only when it exists on the page
        if (strpos($buffer, 'class="directory_location"')!==false) {
          $buffer = str_replace('class="directory_location"', 'class="directory_location updated"', $buffer);
        }

        // Example: Use PHP to process your page title
        $matches=[];
        preg_match('~<title>(.+?)</title>~s', $buffer, $matches);
        if (!empty($matches[1])) { // match title value
          if ($title=explode(' | ',$matches[1])) {
            if (sizeof($title)>1) {
              $title=array_unique($title); // remove repeated elements
              foreach($title as $key=>$val) { // remove empty elements
                if (empty($val)) {
                  unset($title[$key]);
                };
              };
              $title=implode(' | ',$title);
              if (!empty($title) && $title!=$matches[1]) {
                $buffer=preg_replace('~<title>.+?</title>~', '<title>'.$title.'</title>', $buffer,1); // update title if there were changes
              };
            };
          };
        };

        return $buffer;
      }

   }
?>