* @param string $text Text to be balanced.
* @return string Balanced text.
function force_balance_tags( $text ) {
// Known single-entity/self-closing tags.
$single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source' );
// Tags that can be immediately nested within themselves.
$nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' );
// WP bug fix for comments - in case you REALLY meant to type '< !--'.
$text = str_replace( '< !--', '< !--', $text );
// WP bug fix for LOVE <3 (and other situations with '<' before a number).
$text = preg_replace( '#<([0-9]{1})#', '<$1', $text );
* Matches supported tags.
* To get the pattern as a string without the comments paste into a PHP
* @see https://html.spec.whatwg.org/#elements-2
* @see https://w3c.github.io/webcomponents/spec/custom/#valid-custom-element-name
* php > $s = [paste copied contents of expression below including parentheses];
'#<' . // Start with an opening bracket.
'(/?)' . // Group 1 - If it's a closing tag it'll have a leading slash.
'(' . // Group 2 - Tag name.
// Custom element tags have more lenient rules than HTML tag names.
'(?:[a-z](?:[a-z0-9._]*)-(?:[a-z0-9._-]+)+)' .
// Traditional tag rules approximate HTML tag names.
// We either immediately close the tag with its '>' and have nothing here.
'(/?)' . // Group 3 - "attributes" for empty tag.
// Or we must start with space characters to separate the tag name from the attributes (or whitespace).
'(\s+)' . // Group 4 - Pre-attribute whitespace.
'([^>]*)' . // Group 5 - Attributes.
'>#' // End with a closing bracket.
while ( preg_match( $tag_pattern, $text, $regex ) ) {
$has_leading_slash = ! empty( $regex[1] );
$tag = strtolower( $tag_name );
$is_single_tag = in_array( $tag, $single_tags, true );
$pre_attribute_ws = isset( $regex[4] ) ? $regex[4] : '';
$attributes = trim( isset( $regex[5] ) ? $regex[5] : $regex[3] );
$has_self_closer = '/' === substr( $attributes, -1 );
$i = strpos( $text, $full_match );
$l = strlen( $full_match );
if ( $has_leading_slash ) { // End tag.
// If too many closing tags.
// Or close to be safe $tag = '/' . $tag.
// If stacktop value = tag close value, then pop.
} elseif ( $tagstack[ $stacksize - 1 ] === $tag ) { // Found closing tag.
$tag = '</' . $tag . '>'; // Close tag.
} else { // Closing tag not at top, search for it.
for ( $j = $stacksize - 1; $j >= 0; $j-- ) {
if ( $tagstack[ $j ] === $tag ) {
for ( $k = $stacksize - 1; $k >= $j; $k-- ) {
$tagqueue .= '</' . array_pop( $tagstack ) . '>';
if ( $has_self_closer ) { // If it presents itself as a self-closing tag...
// ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such
// and immediately close it with a closing tag (the tag will encapsulate no text as a result).
if ( ! $is_single_tag ) {
$attributes = trim( substr( $attributes, 0, -1 ) ) . "></$tag";
} elseif ( $is_single_tag ) { // Else if it's a known single-entity tag but it doesn't close itself, do so.
} else { // It's not a single-entity tag.
// If the top of the stack is the same as the tag we want to push, close previous tag.
if ( $stacksize > 0 && ! in_array( $tag, $nestable_tags, true ) && $tagstack[ $stacksize - 1 ] === $tag ) {
$tagqueue = '</' . array_pop( $tagstack ) . '>';
$stacksize = array_push( $tagstack, $tag );
if ( $has_self_closer && $is_single_tag ) {
// We need some space - avoid <br/> and prefer <br />.
$tag = '<' . $tag . $pre_attribute_ws . $attributes . '>';
// If already queuing a close tag, then put this tag on too.
if ( ! empty( $tagqueue ) ) {
$newtext .= substr( $text, 0, $i ) . $tag;
$text = substr( $text, $i + $l );
while ( $x = array_pop( $tagstack ) ) {
$newtext .= '</' . $x . '>'; // Add remaining tags to close.
// WP fix for the bug with HTML comments.
$newtext = str_replace( '< !--', '<!--', $newtext );
$newtext = str_replace( '< !--', '< !--', $newtext );
* Acts on text which is about to be edited.
* The $content is run through esc_textarea(), which uses htmlspecialchars()
* to convert special characters to HTML entities. If `$richedit` is set to true,
* it is simply a holder for the {@see 'format_to_edit'} filter.
* @since 4.4.0 The `$richedit` parameter was renamed to `$rich_text` for clarity.
* @param string $content The text about to be edited.
* @param bool $rich_text Optional. Whether `$content` should be considered rich text,
* in which case it would not be passed through esc_textarea().
* @return string The text after the filter (and possibly htmlspecialchars()) has been run.
function format_to_edit( $content, $rich_text = false ) {
* Filters the text to be formatted for editing.
* @param string $content The text, prior to formatting for editing.
$content = apply_filters( 'format_to_edit', $content );
$content = esc_textarea( $content );
* Add leading zeros when necessary.
* If you set the threshold to '4' and the number is '10', then you will get
* back '0010'. If you set the threshold to '4' and the number is '5000', then you
* Uses sprintf to append the amount of zeros based on the $threshold parameter
* and the size of the number. If the number is large enough, then no zeros will
* @param int $number Number to append zeros to if not greater than threshold.
* @param int $threshold Digit places number needs to be to not have zeros added.
* @return string Adds leading zeros to number if needed.
function zeroise( $number, $threshold ) {
return sprintf( '%0' . $threshold . 's', $number );
* Adds backslashes before letters and before a number at the start of a string.
* @param string $string Value to which backslashes will be added.
* @return string String with backslashes inserted.
function backslashit( $string ) {
if ( isset( $string[0] ) && $string[0] >= '0' && $string[0] <= '9' ) {
$string = '\\\\' . $string;
return addcslashes( $string, 'A..Za..z' );
* Appends a trailing slash.
* Will remove trailing forward and backslashes if it exists already before adding
* a trailing forward slash. This prevents double slashing a string or path.
* The primary use of this is for paths and thus should be used for paths. It is
* not restricted to paths and offers no specific path support.
* @param string $string What to add the trailing slash to.
* @return string String with trailing slash added.
function trailingslashit( $string ) {
return untrailingslashit( $string ) . '/';
* Removes trailing forward slashes and backslashes if they exist.
* The primary use of this is for paths and thus should be used for paths. It is
* not restricted to paths and offers no specific path support.
* @param string $string What to remove the trailing slashes from.
* @return string String without the trailing slashes.
function untrailingslashit( $string ) {
return rtrim( $string, '/\\' );
* Adds slashes to escape strings.
* Slashes will first be removed if magic_quotes_gpc is set, see {@link
* https://www.php.net/magic_quotes} for more details.
* @param string $gpc The string returned from HTTP request data.
* @return string Returns a string escaped with slashes.
function addslashes_gpc( $gpc ) {
* Navigates through an array, object, or scalar, and removes slashes from the values.
* @param mixed $value The value to be stripped.
* @return mixed Stripped value.
function stripslashes_deep( $value ) {
return map_deep( $value, 'stripslashes_from_strings_only' );
* Callback function for `stripslashes_deep()` which strips slashes from strings.
* @param mixed $value The array or string to be stripped.
* @return mixed The stripped value.
function stripslashes_from_strings_only( $value ) {
return is_string( $value ) ? stripslashes( $value ) : $value;
* Navigates through an array, object, or scalar, and encodes the values to be used in a URL.
* @param mixed $value The array or string to be encoded.
* @return mixed The encoded value.
function urlencode_deep( $value ) {
return map_deep( $value, 'urlencode' );
* Navigates through an array, object, or scalar, and raw-encodes the values to be used in a URL.
* @param mixed $value The array or string to be encoded.
* @return mixed The encoded value.
function rawurlencode_deep( $value ) {
return map_deep( $value, 'rawurlencode' );
* Navigates through an array, object, or scalar, and decodes URL-encoded values
* @param mixed $value The array or string to be decoded.
* @return mixed The decoded value.
function urldecode_deep( $value ) {
return map_deep( $value, 'urldecode' );
* Converts email addresses characters to HTML entities to block spam bots.
* @param string $email_address Email address.
* @param int $hex_encoding Optional. Set to 1 to enable hex encoding.
* @return string Converted email address.
function antispambot( $email_address, $hex_encoding = 0 ) {
$email_no_spam_address = '';
for ( $i = 0, $len = strlen( $email_address ); $i < $len; $i++ ) {
$j = rand( 0, 1 + $hex_encoding );
$email_no_spam_address .= '&#' . ord( $email_address[ $i ] ) . ';';
$email_no_spam_address .= $email_address[ $i ];
$email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[ $i ] ) ), 2 );
return str_replace( '@', '@', $email_no_spam_address );
* Callback to convert URI match to HTML A element.
* This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
* @param array $matches Single Regex Match.
* @return string HTML A element with URI address.
function _make_url_clickable_cb( $matches ) {
if ( ')' === $matches[3] && strpos( $url, '(' ) ) {
// If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it,
// add the closing parenthesis to the URL. Then we can let the parenthesis balancer do its thing below.
// Include parentheses in the URL only if paired.
while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
$suffix = strrchr( $url, ')' ) . $suffix;
$url = substr( $url, 0, strrpos( $url, ')' ) );
if ( 'comment_text' === current_filter() ) {
* Filters the rel value that is added to URL matches converted to links.
* @param string $rel The rel value.
* @param string $url The matched URL being converted to a link tag.
$rel = apply_filters( 'make_clickable_rel', $rel, $url );
return $matches[1] . "<a href=\"$url\" rel=\"$rel\">$url</a>" . $suffix;
* Callback to convert URL match to HTML A element.
* This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
* @param array $matches Single Regex Match.
* @return string HTML A element with URL address.
function _make_web_ftp_clickable_cb( $matches ) {
$dest = 'http://' . $dest;
// Removed trailing [.,;:)] from URL.
$last_char = substr( $dest, -1 );
if ( in_array( $last_char, array( '.', ',', ';', ':', ')' ), true ) === true ) {
$dest = substr( $dest, 0, strlen( $dest ) - 1 );
$dest = esc_url( $dest );
if ( 'comment_text' === current_filter() ) {
/** This filter is documented in wp-includes/formatting.php */
$rel = apply_filters( 'make_clickable_rel', $rel, $dest );
return $matches[1] . "<a href=\"$dest\" rel=\"$rel\">$dest</a>$ret";
* Callback to convert email address match to HTML A element.
* This function was backported from 2.5.0 to 2.3.2. Regex callback for make_clickable().
* @param array $matches Single Regex Match.
* @return string HTML A element with email address.
function _make_email_clickable_cb( $matches ) {
$email = $matches[2] . '@' . $matches[3];
return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
* Convert plaintext URI to HTML links.
* Converts URI, www and ftp, and email addresses. Finishes by fixing links
* @param string $text Content to convert URIs.
* @return string Content with converted URIs.
function make_clickable( $text ) {
$textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // Split out HTML tags.
$nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>.
foreach ( $textarr as $piece ) {
if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) || preg_match( '|^<script[\s>]|i', $piece ) || preg_match( '|^<style[\s>]|i', $piece ) ) {
} elseif ( $nested_code_pre && ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) || '</script>' === strtolower( $piece ) || '</style>' === strtolower( $piece ) ) ) {
if ( $nested_code_pre || empty( $piece ) || ( '<' === $piece[0] && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
// Long strings might contain expensive edge cases...
if ( 10000 < strlen( $piece ) ) {
foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses.
if ( 2101 < strlen( $chunk ) ) {
$r .= $chunk; // Too big, no whitespace: bail.
$r .= make_clickable( $chunk );
$ret = " $piece "; // Pad with whitespace to simplify the regexes.