Title: tag_escape
Published: April 25, 2014
Last modified: August 20, 2026

---

# tag_escape( string $tag_name ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#changelog)

Escapes an HTML tag name.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#parameters)󠁿

 `$tag_name`stringrequired

## 󠀁[Return](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#return)󠁿

 string Sanitized tag name.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#source)󠁿

    ```php
    function tag_escape( $tag_name ) {
    	$safe_tag = strtolower( preg_replace( '/[^a-zA-Z0-9-_:]/', '', $tag_name ) );
    	/**
    	 * Filters a string cleaned and escaped for output as an HTML tag.
    	 *
    	 * @since 2.8.0
    	 *
    	 * @param string $safe_tag The tag name after it has been escaped.
    	 * @param string $tag_name The text before it was escaped.
    	 */
    	return apply_filters( 'tag_escape', $safe_tag, $tag_name );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/formatting.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/formatting.php#L4880)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/formatting.php#L4880-L4891)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#hooks)󠁿

 [apply_filters( ‘tag_escape’, string $safe_tag, string $tag_name )](https://developer.wordpress.org/reference/hooks/tag_escape/)

Filters a string cleaned and escaped for output as an HTML tag.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [get_tag_regex()](https://developer.wordpress.org/reference/functions/get_tag_regex/)`wp-includes/functions.php` |

Returns RegEx body to liberally match an opening HTML tag.

  | 
| [gallery_shortcode()](https://developer.wordpress.org/reference/functions/gallery_shortcode/)`wp-includes/media.php` |

Builds the Gallery shortcode output.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/tag_escape/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.5.5](https://developer.wordpress.org/reference/since/6.5.5/) | Allow hyphens in tag names (i.e. custom elements). | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Ftag_escape%2F)
before being able to contribute a note or feedback.