Filters 1.9 and before

From MoodleDocs
Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable.

Please note: This page contains information for developers. You may prefer to read the information about filters for teachers and administrators.

Filters allow for the automatic transformation of entered text into different, often more complex forms. For example the titles of Resources can automatically become hyperlinks that take you to the relevant resource, URLs pointing to mp3 files can become Flash controls embedded in the webpage that let you pause and rewind the audio. The possibilities are endless and there are a number of standard filters included with Moodle and many more specialized filters contributed by the community.

To create a filter

To create a filter that removes all occurrences of the letter "x" - we'll call it "removex":

  1. Create a new folder inside Moodle's /filter/ folder, called "removex"
  2. Create a new PHP script file inside the folder you've just created - name it "filter.php"
  3. Write a new PHP function in this file, called "removex_filter()" which takes two parameters - a course ID and a piece of text to be filtered - and returns the processed text.

For our example the filter.php file would look like:

function removex_filter($courseid, $text) {
    return str_replace('x', '', $text);

When trying this out, remember to make sure that you activate the filter in the filters administration screen.

Also remember that text filtering functions, when activated, will be used intensively by the server, so you should optimise the filters as far as possible (cut down on database calls etc). Moodle caches the results of filtering to help with processing speed, but it's still worth being careful about your filter design.

Filters are applied to all text that is printed with the output functions format_text() or format_string(). One thing to keep in mind when designing the filter is that the function format_text() first applies other transformations (for example text_to_html() or replace_smilies()) before the strings are passed to your filter. The function format_string() on the other hand passes the string as it is.

Adding a settings screen

Moodle 1.6 added the options for filters to have their own settings screen. To do this perform the following steps:

  • In the same folder as the filter create a file called filtersettings.php.
  • Add admin_setting objects to the $settings page like this:
$settings->add(new admin_setting_configcheckbox('filter_myfilter/mysetting',
        get_string('mysetting', 'filter_myfilter'),
        get_string('configmysetting', 'filter_myfilter'), 0));

In standard Moodle, the censor, mediaplugin and tex filters have good examples of what you need to do.

A note on language strings

As the sharp-eyed will have noticed in the last section, the language strings for your filter (if any) should go in the file filter/myfilter/lang/en_utf8/filter_myfilter.php, and can be accessed using

get_string('mystring', 'filter_myfilter').

You can copy and paste the following to start your filter_myfilter.php file:

<?php // $Id$
// Language string for filter/myfilter.

$string['filtername'] = 'My magic filter.';

Unfortunately Moodle 1.9 looks for the name of your filter as get_string('mystring', 'myfilter') in the /lang/ folder - note: no 'filter_'. So you have to put a myfilter.php file with the above code for the filter name in the lang\en_utf8\ folder.
This is annoying because that string cannot be in a file inside the filter folder. This has been fixed for Moodle 2.0. See MDL-17684.

See also

  • Filters 2.0 The new version of these instructions for after Moodle 2.0 is released.
  • Filters schema - a page containing some ideas and thoughts about modifications to the filters system
  • Filters