Skip to content

Instantly share code, notes, and snippets.

@koop
Created September 27, 2011 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koop/1246326 to your computer and use it in GitHub Desktop.
Save koop/1246326 to your computer and use it in GitHub Desktop.
Generates a properly escaped HTML string based upon an array of HTML attributes.
<?php
function esc_attrs( $attrs ) {
$html = ' ';
$url_attrs = array( 'src', 'href', 'formaction', 'data', 'action', 'icon', 'manifest', 'poster' );
foreach ( $attrs as $key => $value ) {
// If an attribute starts with 'on', assume it's a javascript parameter
if ( 'on' == substr( $key, 0, 2 ) )
$value = esc_js( $value );
elseif ( in_array( $key, $url_attrs ) )
$value = esc_url( $value );
else
$value = esc_attr( $value );
$html .= esc_html( $key ) . "='" . $value . "' ";
}
return $html;
}
@markjaquith
Copy link

Interesting. Might be worth a browse through core to see how much use we could make of this.

Needs some strtolower() action on the in_array() and "on" checks. And the key should probably be more strictly sanitized than esc_html().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment