Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/sanitize html class


Sanitizes 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 the function will return the alternative value supplied. After sanitize_html_class() has done its work, it passes the sanitized class name through the sanitize_html_class filter.


(string) (required) The classname to be sanitized.
Default: None
(string) (optional) The value to return if the sanitization ends up as an empty string. Defaults to an empty string.
Default: None

Return Values

string (string) 
The sanitized value.


// If you want to explicitly style a post, you can use the sanitized version of the post title as a class
$post_class = sanitize_html_class( $post->post_title );
echo '<div class="' . $post_class . '">';

Change Log

Source File

sanitize_html_class() is located in wp-includes/formatting.php.



sanitize_html_class() is in a class of functions that help you sanitize potentially unsafe data which allow you to pass an arbitrary variable and receive the clean version based on data type. Others include:


See also index of Function Reference and index of Template Tags.