Skip to main content

Introducing “Filters”

Filters are a type of hook that allow you to modify data before it is saved to the database or rendered on the screen. Unlike action hooks, which trigger specific events, filters are used to modify content or data in-place.

So, with “Filters,” you can modify data that is subject to filters or allowed to be modified.

Example:

Based on the screenshot above, you can modify the data or content of the “meta description” using the "meta_description" key. However, you can’t modify the page title (<title>) or get_page_title() data because it does not apply any filters.

Sample code:

function filter_meta_description($data){
	return "Modified: " . $data;
}
add_filter('meta_description', 'filter_meta_description');

Or using anonymous function:

add_filter('meta_description', function($data) {
	return "Modified: " . $data;
});

More robust example with Extra Fields :

add_filter('meta_description', function($data) {
	global $game;
	global $base_taxonomy;
	if($base_taxonomy === 'game'){ // Is single game page
		if($game){
			$value = $game->getExtraField('meta_description');
			if($value){ // Have meta_description value
				return $value;
			}
		}
	}
    return $data;
});

You can put add_filter() in custom.php or your custom theme functions.php

You can also override or modify a data that already been modified by other code.

With the “Filters” feature, upcoming plugins will have the capability to perform advanced tasks such as altering content data and properties.

Filters can be used for:

  1. SEO Optimization
  2. Data Sanitization
  3. Content Formatting
  4. And much more

As of the date this post is published, “17/09/2023,” none of the stock themes have implemented Filters, but they will be implemented soon.

“Filters” are introduced in CloudArcade v1.6.9.