WordPress.org

Codex

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

Function Reference/sanitize html class

Description

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.

Parameters

$class
(string) (required) The classname to be sanitized.
Default: None
$fallback
(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.

Examples

<?php
// 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.

Related

Functions

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:

Filters

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