Performance: Why hooks enable extensibility
Hooks affect how quickly and efficiently WordPress can modify or extend functionality without changing core code, impacting plugin load and execution speed.
<?php
// Using add_action hook to extend functionality
add_action('init', function() {
// custom code here
});
?><?php
// Directly editing core WordPress files to add custom code
function custom_feature() {
// custom code here
}
// Inserted directly in core files
?>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct core file edits | N/A | N/A | Blocks rendering during updates | [X] Bad |
| Using hooks (add_action, add_filter) | Minimal | 0 | Non-blocking script execution | [OK] Good |
add_action to attach functions to action hooks.add_action('hook_name', 'function_name');, so add_action('init', 'my_function'); is correct.add_filter('the_title', function($title) { return strtoupper($title); });
echo apply_filters('the_title', 'Hello World');add_filter attaches a function that changes the title to uppercase.apply_filters runs the attached function on 'Hello World', converting it to 'HELLO WORLD'.add_action('wp_footer', 'footer_message');
function footer_message() {
echo 'Thank you for visiting!';
}footer_message is defined properly and hooked to 'wp_footer'.the_title filter is appropriate.add_filter attaches a function to modify the title, adding ' - Read More' safely without core edits.Pass the test and this pattern moves unknown