Sanitize or escape? Which WordPress function to use where
WordPress has two families of functions for user data, and they are often mixed up. Sanitizing cleans data on the way in, before it is saved. Escaping makes data safe on the way out, for the exact place in the HTML where it is printed. They do different jobs and do not replace each other: you sanitize input and you escape output, both.
Problem: A plugin saves text, a URL, an email address and some HTML from a form and later prints them into attributes, tag content and inline JavaScript. One wrong function means broken data or an XSS hole.
Solution: On save, use the sanitize_*() function for the type of data. On output, use the esc_*() function for the context: esc_html() for text, esc_attr() for attributes, esc_url() for links, wp_json_encode() for JavaScript, wp_kses() for HTML you allow.
Sanitizing input
$_POST values come with slashes added by WordPress, so remove them first with wp_unslash():
<?php
$post = wp_unslash( $_POST );
$name = sanitize_text_field( $post['name'] ?? '' ); // one line of text: no tags, no line breaks
$bio = sanitize_textarea_field( $post['bio'] ?? '' ); // like above, but keeps line breaks
$email = sanitize_email( $post['email'] ?? '' );
$url = esc_url_raw( $post['website'] ?? '' ); // a URL for saving (no HTML encoding)
$key = sanitize_key( $post['mode'] ?? '' ); // lowercase letters, digits, - and _
$class = sanitize_html_class( $post['class'] ?? '' );
$filename = sanitize_file_name( $post['filename'] ?? '' );
$count = absint( $post['count'] ?? 0 );
$color = sanitize_hex_color( $post['color'] ?? '' ); // '#1e73be' or '' (null for invalid)
$content = wp_kses_post( $post['content'] ?? '' ); // HTML allowed in post content
$note = wp_kses( $post['note'] ?? '', array( // only these tags and attributes
'strong' => array(),
'em' => array(),
'a' => array( 'href' => true, 'title' => true ),
) );
Sanitizing is about the type, so validate the meaning as well: an email that passes sanitize_email() may still be empty, a number may be out of range. Check and reject such values instead of saving them.
Escaping output
Escape as late as possible, right where the value is printed, and choose the function by the context:
<?php
$name = get_option( 'ha_name' );
$url = get_option( 'ha_website' );
$color = get_option( 'ha_color' );
?>
<h2><?php echo esc_html( $name ); ?></h2>
<input type="text" name="ha_name" value="<?php echo esc_attr( $name ); ?>">
<a href="<?php echo esc_url( $url ); ?>"><?php esc_html_e( 'Website', 'textdomain' ); ?></a>
<textarea name="ha_bio"><?php echo esc_textarea( get_option( 'ha_bio' ) ); ?></textarea>
<style>.ha-box { border-color: <?php echo sanitize_hex_color( $color ); ?>; }</style>
<script>var haData = <?php echo wp_json_encode( array( 'name' => $name ) ); ?>;</script>
<?php echo wp_kses_post( get_option( 'ha_content' ) ); ?>
esc_attr() is not enough for a href: it would let javascript:alert(1) through. esc_url() also checks the protocol. In CSS, neither of them helps; allow only values you can validate, such as a colour or a number. A real-life case of output that needed escaping is in a layout broken by HTML in ACF fields.
The common mistakes
- Escaping on save (
esc_html()beforeupdate_option()): the stored value contains&, and the next save encodes it again. - Sanitizing instead of escaping on output:
sanitize_text_field()is not made for HTML context and does not protect attributes. - Trusting values from the database. They may come from an import, an older version of the plugin or another plugin; escape them like any other input.
esc_url()for redirects and HTTP requests: it encodes&for HTML. Useesc_url_raw()there.
NOTE: The rule fits in one line: sanitize when you receive data, escape when you print it, and do both every time. A value that was sanitized on save still has to be escaped on output, because sanitizing does not know where the value will be printed.