Created
September 27, 2011 21:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting. Might be worth a browse through core to see how much use we could make of this.
Needs some
strtolower()
action on thein_array()
and "on" checks. And the key should probably be more strictly sanitized thanesc_html()
.