Performance: add_action and add_filter
These hooks affect how fast WordPress processes and renders content by adding custom code during page load or interaction.
add_filter('the_content', function($content) { return $content . ' '; });
add_filter('the_content', function($content) { for ($i=0; $i<1000; $i++) { $content .= ' '; } return $content; });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy hook with loops | N/A (server-side) | N/A | N/A | [X] Bad |
| Light hook with simple string append | N/A (server-side) | N/A | N/A | [OK] Good |
add_action in WordPress?add_action doesadd_action hooks your custom function to WordPress events so it runs at the right time.add_action.function change_title($title) {
return 'Hello: ' . $title;
}
add_filter('the_title', 'change_title');
the_title() for a post titled "World"?change_title adds "Hello: " before the original title.the_title() runs, WordPress applies the filter, so "World" becomes "Hello: World".add_filter('the_content', 'modify_content');
function modify_content() {
return 'Modified content';
}modify_content has no parameters, so it cannot receive the content to change.add_filter?add_filter('the_content', ...) is correct because it changes content before display.Pass the test and this pattern moves unknown