Performance: Filter hooks
Filter hooks affect how WordPress modifies data before output, impacting server processing time and page generation speed.
<?php add_filter('the_content', function($content) { return str_replace('foo', 'bar', $content); }); ?>
<?php add_filter('the_content', function($content) { sleep(1); return $content; }); ?>
| Pattern | Server Processing | Delay | Impact on LCP | Verdict |
|---|---|---|---|---|
| Heavy filter with sleep or DB calls | High CPU and wait | Blocks 1000ms+ | Delays LCP significantly | [X] Bad |
| Simple string replacement filter | Minimal CPU | Blocks <10ms | Negligible LCP impact | [OK] Good |
add_filter with the hook name as a string and the callback function name as a string.add_filter with both parameters as strings, which is correct. add_action('the_title', 'my_title_filter'); uses add_action, which is for actions, not filters. filter_add('the_title', 'my_title_filter'); uses a wrong function name. add_filter('the_title', my_title_filter); misses quotes around the callback function name.function add_exclamation($text) {
return $text . '!';
}
add_filter('the_title', 'add_exclamation');
$title = apply_filters('the_title', 'Hello');
echo $title;add_exclamation adds an exclamation mark to the text. It is hooked to 'the_title' filter.apply_filters('the_title', 'Hello') runs the filter function, returning 'Hello!'.function change_content($content) {
echo strtoupper($content);
}
add_filter('the_content', 'change_content');echo instead of returning the modified content, so WordPress receives no changed value.function add_read_more($excerpt) {
// Your code here
}
add_filter('get_the_excerpt', 'add_read_more');Pass the test and this pattern moves unknown