Moodle filters make it possible to automatically transform your content before output or display. For example, filters make it possible for links to media files to be converted automatically into applets for playing the media, they can render embedded equations into images, can convent glossary terms automatically into links and many other things. There is no limit to the number of things that can be achieved using filters.
To get the best results when creating Moodle filters, you have to ensure that you only follow these steps.
1. Set text cache lifetime to ‘0’
Before you do anything else, make sure that text cache lifetime is set to 0. This can be done by going to site administration, then to plugins, then to filters and finally to common filter settings. This setting helps to make it possible to see changes in effect when you make changes to your filter’s code. Otherwise, if you do not set text cache lifetime to 0, you will not be able to see any effect when you make changes to your filter’s code. This can make things very difficult when editing since you will not be able to see what is happening as you are editing filter’s code.
2. Give your filter a memorable name
Ensure that you give your filter a name that you can easily remember. To try your new filter, you have to first log in as the administrator and enable it. This can be done by going to page administration, then to plugins, then to filter and finally to manage filters.
3. Try out your filter
Switch to the filter administration screen. Reload the page to see if it appears with the proper name you have just set. Turn on the filter. The filter is applied to all the text that is printed with the output function. To be able to see the action of you filter add in some more content. For example, you can create a course description and look at it at the course listing to see if the filter has transformed it.
4. Cache the data in the database
This is a very important thing that you have to do when creating a filter. Remember, the filter will be called out to transform all text output. If you are not careful to change the data in the database, you can cause a big performance problem. Hence, if you will need to get data out of a database, cache it to ensure that you only have a defined number of database queries when you load a page once.
5. Perform local configurations
In each context, the filters can have different configurations. For example, you can do glossary changes so that in forum 1, you only link to words that are present in that particular glossary. If you have another forum, say, forum 2, you can link only words from glossary 2.
These are some of the best practices that should be followed when creating Moodle filters. They are all simple and easy to follow. They can really help you have an easy experience when creating Moodle filters.