Mecha CMS

Mecha CMS blog and documentation.

Class Filter

Updated: Sunday, 07 August 2016

Filter hooks.

Apply a Filter

Firstly, declare the Filter::apply method on the source text you want to manipulate:

$html = 'Lorem ipsum dolor sit amet.';
$output = Filter::apply('my_filter', $html);

echo $output;

Add a Filter

Now, in another place before the declaration above, for example in your functions.php file, declare this filter:

Filter::add('my_filter', function($str) {
    return str_replace('ipsum', 'test!', $str);
}, 10);

10 is the filter execution priority. The smaller the value, the earlier the filter will be executed. And filters with the same priority are executed in the order in which they were added to the filter. Default is 10.

Now, the outputted text would be replaced into this:

Lorem test! dolor sit amet.

Check if Filter Already Exist

Added since version 1.0.3.

if(Filter::exist('content')) {
    echo 'OK.';

Remove a Filter

Added since version 1.0.3.

Filter::apply('content', $test);

The text inside $test variable above will not be affected by the content filter because the content filter already removed.

Remove Filter by Priority

Filter::remove('content', 4);

Remove Filter by Function Name

Filter::remove('content', 'my_content_fn');
Donation and Email Subscription