Skip to content

Instantly share code, notes, and snippets.

@whitskyler
Created September 10, 2012 19:44
Show Gist options
  • Select an option

  • Save whitskyler/3693344 to your computer and use it in GitHub Desktop.

Select an option

Save whitskyler/3693344 to your computer and use it in GitHub Desktop.
Data Sanitization with WordPress' esc_attr()
<?php
// This is our malicious script
$attr = '"onclick="alert(\'Oops, hacked by XSS!\');';
// This is our escaped, safe attribute
$safe_attr = esc_attr( $attr );
?>
<h1 class="entry-title"><?php _e( 'Testing esc_attr()', 'whit' ); ?></h1>
<p>
<a href="#" title="<?php echo $attr; ?>">
<?php _e( 'Example without esc_attr()', 'whit' ); ?>
</a>
<br>
<a href="#" title="<?php echo $safe_attr; ?>">
<?php _e( 'Example with esc_attr()', 'whit' ); ?>
</a>
</p>
<form id="test" action="">
First Name: <input name="firstname" type="text" value="<?php echo $attr; ?>"/>
<br>
Last Name: <input name="lastname" type="text" value="<?php echo $safe_attr; ?>"/>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment