Performance: Data sanitization
Data sanitization affects page load speed indirectly by preventing unnecessary server processing and reducing security risks that can cause slowdowns.
<?php $safe_input = sanitize_text_field($_POST['user_input']); $wpdb->insert('wp_table', ['data' => $safe_input]); ?>
<?php $unsafe_input = $_POST['user_input']; $query = $wpdb->prepare("INSERT INTO wp_table (data) VALUES (%s)", $unsafe_input); $wpdb->query($query); ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No sanitization, direct input | N/A | N/A | N/A | [X] Bad |
| Sanitize input with WordPress functions | N/A | N/A | N/A | [OK] Good |
sanitize_text_field() cleans plain text input by stripping tags and harmful characters.esc_html() escapes HTML for output, wp_kses_post() allows safe HTML, sanitize_email() cleans emails.$input = "Hello"; $clean = sanitize_text_field($input); echo $clean;
$url = $_POST['website'];
$clean_url = sanitize_text_field($url);
if (filter_var($clean_url, FILTER_VALIDATE_URL)) {
echo 'Valid URL';
} else {
echo 'Invalid URL';
}wp_kses_post() allows safe HTML tags and removes harmful ones, ideal for user HTML input.esc_html() escapes HTML entities to prevent XSS when displaying content.wp_kses_post(), then escape output with esc_html() for safety.Pass the test and this pattern moves unknown