« All Handlers

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: 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$_LW->REGISTERED_APPS['my_app']=array(
'title'=>'My App',
'handlers'=>array('onOutput'),
);

class LiveWhaleApplicationMyApp {

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

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

return $buffer;
}

}
?>

onOuput Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
$_LW->REGISTERED_APPS['my_app']=array(
'title'=>'My App',
'handlers'=>array('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=array();
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;
}

}
?>