What is the difference between sanitizing, validating and escaping in WordPress?
| Step | When | Example |
|---|---|---|
| Validate | Check, reject if wrong | is_email( $email ) |
| Sanitize | Clean the input before saving | sanitize_text_field( $_POST['name'] ) |
| Escape | Make safe when printing | esc_html( $name ), esc_url( $url ), esc_attr( $value ) |
$input = '<script>alert(1)</script>Riya';
echo 'Sanitize (save): ' . sanitize_text_field( $input ) . "\n";
echo 'Escape (print): ' . esc_html( $input ) . "\n";
echo 'Validate: ' . var_export( is_email( 'riya@example' ), true );
Output
Sanitize (save): Riya
Escape (print): <script>alert(1)</script>Riya
Validate: false
Remember: sanitize early, escape late. Details are in Data Validation, Sanitizing and Escaping.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |