Performance: Data escaping (output)
Data escaping affects page security and rendering stability by preventing injection attacks and layout breaks.
<?php echo esc_html( $user_input ); ?>
<?php echo $user_input; ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Raw output without escaping | Potentially complex if injected HTML adds nodes | Multiple reflows if layout shifts occur | Higher paint cost due to unstable layout | [X] Bad |
| Output with esc_html() escaping | Stable DOM nodes count | Single layout pass | Minimal paint cost with stable layout | [OK] Good |
esc_html() in WordPress?esc_url() specifically to clean URLs before output.esc_html() is for HTML content, esc_attr() for attributes, and sanitize_text_field() is for input sanitizing, not output escaping.$user_input = '<script>alert(1)</script>'; and we run echo esc_html($user_input);?echo $user_name; where $user_name comes from user input. What is the problem and how to fix it?esc_html() is the proper function to escape special characters safely.<a href="...">. Which is the best way to escape it to keep your site safe and valid?esc_url() sanitizes URL schemes (e.g., strips javascript:) and escapes for safe HTML attribute output.esc_attr() only escapes HTML chars but allows dangerous schemes like javascript:alert(1), leading to XSS.Pass the test and this pattern moves unknown