What is the difference between sanitizing, validating and escaping in WordPress?

StepWhenExample
ValidateCheck, reject if wrongis_email( $email )
SanitizeClean the input before savingsanitize_text_field( $_POST['name'] )
EscapeMake safe when printingesc_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): &lt;script&gt;alert(1)&lt;/script&gt;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.