Performance: Common action hooks
This affects how and when code runs during page load and user interaction, impacting load speed and responsiveness.
<?php add_action('wp_enqueue_scripts', function() { wp_enqueue_script('custom-js'); }); ?>
<?php add_action('wp_head', function() { heavy_database_query(); }); ?>| Pattern | Server Processing | Rendering Delay | User Interaction Impact | Verdict |
|---|---|---|---|---|
| Heavy action hook on 'wp_head' | High CPU usage | Blocks rendering | Delays interactivity | [X] Bad |
| Proper script enqueue on 'wp_enqueue_scripts' | Minimal CPU usage | Non-blocking | Fast interactivity | [OK] Good |
init hookinit hook runs after WordPress loads but before headers are sent, ideal for initialization tasks.wp_head and wp_footer run during page rendering, save_post runs on post save, so they don't fit the timing.init -> Option Dinit [OK]init runs early after loading [OK]wp_head with initsave_post runs on page loadwp_footer timing with initmy_custom_function to the wp_footer action hook?add_action('wp_head', function() { echo '<!-- Header comment -->'; });wp_head hook timingwp_head hook runs inside the HTML <head> section of the page.<head>.wp_head outputs inside <head> [OK]wp_head outputs inside <head> tag [OK]add_action('save_post', 'save_custom_data');
function save_custom_data() {
update_post_meta(get_the_ID(), 'custom_key', 'custom_value');
}save_post hook passes the post ID as a parameter to the callback function, so the function must accept it.get_the_ID() may not return the correct post ID in this context; the passed post ID parameter should be used instead.wp_footer hook is the proper place to add scripts before the closing </body> tag.is_front_page() to ensure the script runs only on the front page.add_action('wp_footer', 'my_script_function') and inside the function check is_front_page() before outputting the script. -> Option Aadd_action('wp_footer', 'my_script_function') and inside the function check is_front_page() before outputting the script. [OK]Pass the test and this pattern moves unknown