On this page
sanitize_html_class( string $class, string $fallback = '' ): string
Sanitizes an HTML classname to ensure it only contains valid characters.
Description
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.
Parameters
$classstring Required-
The classname to be sanitized
$fallbackstring Optional-
The value to return if the sanitization ends up as an empty string.
Defaults to an empty string.Default:
''
Return
string The sanitized value
Source
File: wp-includes/formatting.php. View all references
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 && $fallback ) {
return sanitize_html_class( $fallback );
}
/**
* Filters a sanitized HTML class string.
*
* @since 2.8.0
*
* @param string $sanitized The sanitized HTML class.
* @param string $class HTML class before sanitization.
* @param string $fallback The fallback string.
*/
return apply_filters( 'sanitize_html_class', $sanitized, $class, $fallback );
}
Hooks
- apply_filters( 'sanitize_html_class',
string $sanitized ,string $class ,string $fallback ) -
Filters a sanitized HTML class string.
Related
Uses
| Uses | Description |
|---|---|
| sanitize_html_class() wp-includes/formatting.php | Sanitizes an HTML classname to ensure it only contains valid characters. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| WP_Theme_JSON::remove_insecure_settings() wp-includes/class-wp-theme-json.php | Processes a setting node and returns the same node without the insecure settings. |
| WP_Media_List_Table::column_title() wp-admin/includes/class-wp-media-list-table.php | Handles the title column output. |
| _navigation_markup() wp-includes/link-template.php | Wraps passed links in navigational markup. |
| login_header() wp-login.php | Output the login page header. |
| WP_Screen::add_help_tab() wp-admin/includes/class-wp-screen.php | Adds a help tab to the contextual help for the screen. |
| WP_Plugin_Install_List_Table::display_rows() wp-admin/includes/class-wp-plugin-install-list-table.php | |
| iframe_header() wp-admin/includes/template.php | Generic Iframe header for use with Thickbox. |
| attachment_submitbox_metadata() wp-admin/includes/media.php | Displays non-editable attachment metadata in the publish meta box. |
| _wp_menu_output() wp-admin/menu-header.php | Display menu. |
| sanitize_html_class() wp-includes/formatting.php | Sanitizes an HTML classname to ensure it only contains valid characters. |
| get_post_class() wp-includes/post-template.php | Retrieves an array of the class names for the post container element. |
| get_body_class() wp-includes/post-template.php | Retrieves an array of the class names for the body element. |
| gallery_shortcode() wp-includes/media.php | Builds the Gallery shortcode output. |
| img_caption_shortcode() wp-includes/media.php | Builds the Caption shortcode output. |
| get_comment_class() wp-includes/comment-template.php | Returns the classes for the comment div as an array. |
| _WP_Editors::editor_settings() wp-includes/class-wp-editor.php |
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/sanitize_html_class