function sanitize_html_class( $class, $fallback = '' ) {}

Santizes a html classname to ensure it only contains valid characters
Strips the string down to A-Z,a-z,0-9,_,-. If this results in an empty string then it will return the alternative value supplied.


  • string $class: The classname to be sanitized
  • string $fallback: Optional. The value to return if the sanitization end’s up as an empty string. Defaults to an empty string.

Return values

returns:The sanitized value

Defined filters

  • sanitize_html_class
    apply_filters( 'sanitize_html_class', $sanitized, $class, $fallback )

Source code

function sanitize_html_class( $class, $fallback = '' ) {

	//Strip out any % encoded octets

	$sanitized = preg_replace( '|%[a-fA-F0-9][a-fA-F0-9]|', '', $class );

	//Limit to A-Z,a-z,0-9,_,-

	$sanitized = preg_replace( '/[^A-Za-z0-9_-]/', '', $sanitized );

	if ( '' == $sanitized )

		$sanitized = $fallback;

	return apply_filters( 'sanitize_html_class', $sanitized, $class, $fallback );



No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: