Performance: Plugin security (nonces, sanitization)
This concept affects page load speed indirectly by preventing unnecessary server processing and reducing security risks that could cause slowdowns or crashes.
<?php // Check nonce and sanitize input if (isset($_POST['my_nonce']) && wp_verify_nonce($_POST['my_nonce'], 'my_action')) { $value = sanitize_text_field($_POST['data'] ?? ''); update_option('my_option', $value); } ?>
<?php // No nonce check and no sanitization if (isset($_POST['data'])) { $value = $_POST['data']; update_option('my_option', $value); } ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No nonce or sanitization | 0 | 0 | 0 | [X] Bad |
| With nonce and sanitization | 0 | 0 | 0 | [OK] Good |
wp_verify_nonce().wp_verify_nonce($_POST['my_nonce'], 'my_action').if ( ! wp_verify_nonce($_POST['nonce'], 'save_data') ) {
echo 'Invalid nonce';
} else {
$name = sanitize_text_field($_POST['name']);
echo 'Hello, ' . $name;
}$_POST['name'] = '<script>alert(1)</script>John'?<script>, so the input becomes 'alert(1)John' because sanitize_text_field removes tags but leaves the text inside.if ( ! wp_verify_nonce($_POST['nonce'], 'save_data') ) {
wp_die('Security check failed');
}
$name = sanitize_text_field($_POST['name']);
update_option('user_name', $name);// 1. Verify nonce
if ( ! wp_verify_nonce($_POST['nonce'], 'save_email') ) {
wp_die('Invalid request');
}
// 2. Sanitize input
$email = ???
// 3. Save email
update_option('user_email', $email);sanitize_email() is the correct function to clean and validate email format.sanitize_text_field() is for plain text, esc_html() is for output escaping, and wp_strip_all_tags() removes tags but does not validate email.Pass the test and this pattern moves unknown