Sanitize All User Input with Type-Appropriate Functions
Share
Raw $_GET/$_POST/$_REQUEST data can contain anything. WordPress provides type-specific sanitization functions — use the right one for each data type before storage or use. [CWE-20 · A03:2021]
Why This Matters
prevents injection attacks and data corruption by ensuring input matches expected types
Sanitize All User Input with Type-Appropriate Functions
Impact: HIGH (prevents injection attacks and data corruption by ensuring input matches expected types)
WordPress provides specialized sanitization functions for different data types. Using the wrong one — or none at all — leaves your code vulnerable to XSS, SQL injection, and data corruption. The rule: sanitize on input (before storage), escape on output (before display).
Always call wp_unslash() before sanitizing superglobal data, because WordPress automatically adds slashes via wp_magic_quotes().
Incorrect (raw superglobals):
// ❌ Raw input stored directlyupdate_option( 'my_title', $_POST['title'] );update_post_meta( $post_id, 'email', $_POST['email'] );$page = $_GET['page'];$ids = $_POST['selected_ids'];$content = $_POST['description'];// ❌ Wrong sanitization for the data type$email = sanitize_text_field( $_POST['email'] ); // Doesn't validate email format$url = sanitize_text_field( $_POST['website'] ); // Doesn't validate URL structure$id = sanitize_text_field( $_POST['post_id'] ); // String sanitizer on an integer