wp_specialchars_decode

The timeline below displays how wordpress function wp_specialchars_decode has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Converts a number of HTML entities into their special characters.
 *
 * Specifically deals with: `&`, `<`, `>`, `"`, and `'`.
 *
 * `$quote_style` can be set to ENT_COMPAT to decode `"` entities,
 * or ENT_QUOTES to do both `"` and `'`. Default is ENT_NOQUOTES where no quotes are decoded.
 *
 * @since 2.8.0
 *
 * @param string     $text        The text which is to be decoded.
 * @param string|int $quote_style Optional. Converts double quotes if set to ENT_COMPAT,
 *                                both single and double if set to ENT_QUOTES or
 *                                none if set to ENT_NOQUOTES.
 *                                Also compatible with old _wp_specialchars() values;
 *                                converting single quotes if set to 'single',
 *                                double if set to 'double' or both if otherwise set.
 *                                Default is ENT_NOQUOTES.
 * @return string The decoded text without HTML entities.
 */
function wp_specialchars_decode($text, $quote_style = ENT_NOQUOTES)
{
    $text = (string) $text;
    if (0 === strlen($text)) {
        return '';
    }
    // Don't bother if there are no entities - saves a lot of processing.
    if (!str_contains($text, '&')) {
        return $text;
    }
    // Match the previous behavior of _wp_specialchars() when the $quote_style is not an accepted value.
    if (empty($quote_style)) {
        $quote_style = ENT_NOQUOTES;
    } elseif (!in_array($quote_style, array(0, 2, 3, 'single', 'double'), true)) {
        $quote_style = ENT_QUOTES;
    }
    // More complete than get_html_translation_table( HTML_SPECIALCHARS ).
    $single = array('&#039;' => '\'', '&#x27;' => '\'');
    $single_preg = array('/&#0*39;/' => '&#039;', '/&#x0*27;/i' => '&#x27;');
    $double = array('&quot;' => '"', '&#034;' => '"', '&#x22;' => '"');
    $double_preg = array('/&#0*34;/' => '&#034;', '/&#x0*22;/i' => '&#x22;');
    $others = array('&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&');
    $others_preg = array('/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;');
    if (ENT_QUOTES === $quote_style) {
        $translation = array_merge($single, $double, $others);
        $translation_preg = array_merge($single_preg, $double_preg, $others_preg);
    } elseif (ENT_COMPAT === $quote_style || 'double' === $quote_style) {
        $translation = array_merge($double, $others);
        $translation_preg = array_merge($double_preg, $others_preg);
    } elseif ('single' === $quote_style) {
        $translation = array_merge($single, $others);
        $translation_preg = array_merge($single_preg, $others_preg);
    } elseif (ENT_NOQUOTES === $quote_style) {
        $translation = $others;
        $translation_preg = $others_preg;
    }
    // Remove zero padding on numeric entities.
    $text = preg_replace(array_keys($translation_preg), array_values($translation_preg), $text);
    // Replace characters according to translation table.
    return strtr($text, $translation);
}

WordPress Version: 6.2

/**
 * Converts a number of HTML entities into their special characters.
 *
 * Specifically deals with: `&`, `<`, `>`, `"`, and `'`.
 *
 * `$quote_style` can be set to ENT_COMPAT to decode `"` entities,
 * or ENT_QUOTES to do both `"` and `'`. Default is ENT_NOQUOTES where no quotes are decoded.
 *
 * @since 2.8.0
 *
 * @param string     $text        The text which is to be decoded.
 * @param string|int $quote_style Optional. Converts double quotes if set to ENT_COMPAT,
 *                                both single and double if set to ENT_QUOTES or
 *                                none if set to ENT_NOQUOTES.
 *                                Also compatible with old _wp_specialchars() values;
 *                                converting single quotes if set to 'single',
 *                                double if set to 'double' or both if otherwise set.
 *                                Default is ENT_NOQUOTES.
 * @return string The decoded text without HTML entities.
 */
function wp_specialchars_decode($text, $quote_style = ENT_NOQUOTES)
{
    $text = (string) $text;
    if (0 === strlen($text)) {
        return '';
    }
    // Don't bother if there are no entities - saves a lot of processing.
    if (strpos($text, '&') === false) {
        return $text;
    }
    // Match the previous behavior of _wp_specialchars() when the $quote_style is not an accepted value.
    if (empty($quote_style)) {
        $quote_style = ENT_NOQUOTES;
    } elseif (!in_array($quote_style, array(0, 2, 3, 'single', 'double'), true)) {
        $quote_style = ENT_QUOTES;
    }
    // More complete than get_html_translation_table( HTML_SPECIALCHARS ).
    $single = array('&#039;' => '\'', '&#x27;' => '\'');
    $single_preg = array('/&#0*39;/' => '&#039;', '/&#x0*27;/i' => '&#x27;');
    $double = array('&quot;' => '"', '&#034;' => '"', '&#x22;' => '"');
    $double_preg = array('/&#0*34;/' => '&#034;', '/&#x0*22;/i' => '&#x22;');
    $others = array('&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&');
    $others_preg = array('/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;');
    if (ENT_QUOTES === $quote_style) {
        $translation = array_merge($single, $double, $others);
        $translation_preg = array_merge($single_preg, $double_preg, $others_preg);
    } elseif (ENT_COMPAT === $quote_style || 'double' === $quote_style) {
        $translation = array_merge($double, $others);
        $translation_preg = array_merge($double_preg, $others_preg);
    } elseif ('single' === $quote_style) {
        $translation = array_merge($single, $others);
        $translation_preg = array_merge($single_preg, $others_preg);
    } elseif (ENT_NOQUOTES === $quote_style) {
        $translation = $others;
        $translation_preg = $others_preg;
    }
    // Remove zero padding on numeric entities.
    $text = preg_replace(array_keys($translation_preg), array_values($translation_preg), $text);
    // Replace characters according to translation table.
    return strtr($text, $translation);
}

WordPress Version: 6.1

/**
 * Converts a number of HTML entities into their special characters.
 *
 * Specifically deals with: `&`, `<`, `>`, `"`, and `'`.
 *
 * `$quote_style` can be set to ENT_COMPAT to decode `"` entities,
 * or ENT_QUOTES to do both `"` and `'`. Default is ENT_NOQUOTES where no quotes are decoded.
 *
 * @since 2.8.0
 *
 * @param string     $string The text which is to be decoded.
 * @param string|int $quote_style Optional. Converts double quotes if set to ENT_COMPAT,
 *                                both single and double if set to ENT_QUOTES or
 *                                none if set to ENT_NOQUOTES.
 *                                Also compatible with old _wp_specialchars() values;
 *                                converting single quotes if set to 'single',
 *                                double if set to 'double' or both if otherwise set.
 *                                Default is ENT_NOQUOTES.
 * @return string The decoded text without HTML entities.
 */
function wp_specialchars_decode($string, $quote_style = ENT_NOQUOTES)
{
    $string = (string) $string;
    if (0 === strlen($string)) {
        return '';
    }
    // Don't bother if there are no entities - saves a lot of processing.
    if (strpos($string, '&') === false) {
        return $string;
    }
    // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value.
    if (empty($quote_style)) {
        $quote_style = ENT_NOQUOTES;
    } elseif (!in_array($quote_style, array(0, 2, 3, 'single', 'double'), true)) {
        $quote_style = ENT_QUOTES;
    }
    // More complete than get_html_translation_table( HTML_SPECIALCHARS ).
    $single = array('&#039;' => '\'', '&#x27;' => '\'');
    $single_preg = array('/&#0*39;/' => '&#039;', '/&#x0*27;/i' => '&#x27;');
    $double = array('&quot;' => '"', '&#034;' => '"', '&#x22;' => '"');
    $double_preg = array('/&#0*34;/' => '&#034;', '/&#x0*22;/i' => '&#x22;');
    $others = array('&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&');
    $others_preg = array('/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;');
    if (ENT_QUOTES === $quote_style) {
        $translation = array_merge($single, $double, $others);
        $translation_preg = array_merge($single_preg, $double_preg, $others_preg);
    } elseif (ENT_COMPAT === $quote_style || 'double' === $quote_style) {
        $translation = array_merge($double, $others);
        $translation_preg = array_merge($double_preg, $others_preg);
    } elseif ('single' === $quote_style) {
        $translation = array_merge($single, $others);
        $translation_preg = array_merge($single_preg, $others_preg);
    } elseif (ENT_NOQUOTES === $quote_style) {
        $translation = $others;
        $translation_preg = $others_preg;
    }
    // Remove zero padding on numeric entities.
    $string = preg_replace(array_keys($translation_preg), array_values($translation_preg), $string);
    // Replace characters according to translation table.
    return strtr($string, $translation);
}

WordPress Version: 5.4

/**
 * Converts a number of HTML entities into their special characters.
 *
 * Specifically deals with: &, <, >, ", and '.
 *
 * $quote_style can be set to ENT_COMPAT to decode " entities,
 * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
 *
 * @since 2.8.0
 *
 * @param string     $string The text which is to be decoded.
 * @param string|int $quote_style Optional. Converts double quotes if set to ENT_COMPAT,
 *                                both single and double if set to ENT_QUOTES or
 *                                none if set to ENT_NOQUOTES.
 *                                Also compatible with old _wp_specialchars() values;
 *                                converting single quotes if set to 'single',
 *                                double if set to 'double' or both if otherwise set.
 *                                Default is ENT_NOQUOTES.
 * @return string The decoded text without HTML entities.
 */
function wp_specialchars_decode($string, $quote_style = ENT_NOQUOTES)
{
    $string = (string) $string;
    if (0 === strlen($string)) {
        return '';
    }
    // Don't bother if there are no entities - saves a lot of processing.
    if (strpos($string, '&') === false) {
        return $string;
    }
    // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value.
    if (empty($quote_style)) {
        $quote_style = ENT_NOQUOTES;
    } elseif (!in_array($quote_style, array(0, 2, 3, 'single', 'double'), true)) {
        $quote_style = ENT_QUOTES;
    }
    // More complete than get_html_translation_table( HTML_SPECIALCHARS ).
    $single = array('&#039;' => '\'', '&#x27;' => '\'');
    $single_preg = array('/&#0*39;/' => '&#039;', '/&#x0*27;/i' => '&#x27;');
    $double = array('&quot;' => '"', '&#034;' => '"', '&#x22;' => '"');
    $double_preg = array('/&#0*34;/' => '&#034;', '/&#x0*22;/i' => '&#x22;');
    $others = array('&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&');
    $others_preg = array('/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;');
    if (ENT_QUOTES === $quote_style) {
        $translation = array_merge($single, $double, $others);
        $translation_preg = array_merge($single_preg, $double_preg, $others_preg);
    } elseif (ENT_COMPAT === $quote_style || 'double' === $quote_style) {
        $translation = array_merge($double, $others);
        $translation_preg = array_merge($double_preg, $others_preg);
    } elseif ('single' === $quote_style) {
        $translation = array_merge($single, $others);
        $translation_preg = array_merge($single_preg, $others_preg);
    } elseif (ENT_NOQUOTES === $quote_style) {
        $translation = $others;
        $translation_preg = $others_preg;
    }
    // Remove zero padding on numeric entities.
    $string = preg_replace(array_keys($translation_preg), array_values($translation_preg), $string);
    // Replace characters according to translation table.
    return strtr($string, $translation);
}

WordPress Version: 4.3

/**
 * Converts a number of HTML entities into their special characters.
 *
 * Specifically deals with: &, <, >, ", and '.
 *
 * $quote_style can be set to ENT_COMPAT to decode " entities,
 * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
 *
 * @since 2.8.0
 *
 * @param string     $string The text which is to be decoded.
 * @param string|int $quote_style Optional. Converts double quotes if set to ENT_COMPAT,
 *                                both single and double if set to ENT_QUOTES or
 *                                none if set to ENT_NOQUOTES.
 *                                Also compatible with old _wp_specialchars() values;
 *                                converting single quotes if set to 'single',
 *                                double if set to 'double' or both if otherwise set.
 *                                Default is ENT_NOQUOTES.
 * @return string The decoded text without HTML entities.
 */
function wp_specialchars_decode($string, $quote_style = ENT_NOQUOTES)
{
    $string = (string) $string;
    if (0 === strlen($string)) {
        return '';
    }
    // Don't bother if there are no entities - saves a lot of processing
    if (strpos($string, '&') === false) {
        return $string;
    }
    // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
    if (empty($quote_style)) {
        $quote_style = ENT_NOQUOTES;
    } elseif (!in_array($quote_style, array(0, 2, 3, 'single', 'double'), true)) {
        $quote_style = ENT_QUOTES;
    }
    // More complete than get_html_translation_table( HTML_SPECIALCHARS )
    $single = array('&#039;' => '\'', '&#x27;' => '\'');
    $single_preg = array('/&#0*39;/' => '&#039;', '/&#x0*27;/i' => '&#x27;');
    $double = array('&quot;' => '"', '&#034;' => '"', '&#x22;' => '"');
    $double_preg = array('/&#0*34;/' => '&#034;', '/&#x0*22;/i' => '&#x22;');
    $others = array('&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&');
    $others_preg = array('/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;');
    if ($quote_style === ENT_QUOTES) {
        $translation = array_merge($single, $double, $others);
        $translation_preg = array_merge($single_preg, $double_preg, $others_preg);
    } elseif ($quote_style === ENT_COMPAT || $quote_style === 'double') {
        $translation = array_merge($double, $others);
        $translation_preg = array_merge($double_preg, $others_preg);
    } elseif ($quote_style === 'single') {
        $translation = array_merge($single, $others);
        $translation_preg = array_merge($single_preg, $others_preg);
    } elseif ($quote_style === ENT_NOQUOTES) {
        $translation = $others;
        $translation_preg = $others_preg;
    }
    // Remove zero padding on numeric entities
    $string = preg_replace(array_keys($translation_preg), array_values($translation_preg), $string);
    // Replace characters according to translation table
    return strtr($string, $translation);
}

WordPress Version: 3.7

/**
 * Converts a number of HTML entities into their special characters.
 *
 * Specifically deals with: &, <, >, ", and '.
 *
 * $quote_style can be set to ENT_COMPAT to decode " entities,
 * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
 *
 * @since 2.8.0
 *
 * @param string $string The text which is to be decoded.
 * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old _wp_specialchars() values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
 * @return string The decoded text without HTML entities.
 */
function wp_specialchars_decode($string, $quote_style = ENT_NOQUOTES)
{
    $string = (string) $string;
    if (0 === strlen($string)) {
        return '';
    }
    // Don't bother if there are no entities - saves a lot of processing
    if (strpos($string, '&') === false) {
        return $string;
    }
    // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
    if (empty($quote_style)) {
        $quote_style = ENT_NOQUOTES;
    } elseif (!in_array($quote_style, array(0, 2, 3, 'single', 'double'), true)) {
        $quote_style = ENT_QUOTES;
    }
    // More complete than get_html_translation_table( HTML_SPECIALCHARS )
    $single = array('&#039;' => '\'', '&#x27;' => '\'');
    $single_preg = array('/&#0*39;/' => '&#039;', '/&#x0*27;/i' => '&#x27;');
    $double = array('&quot;' => '"', '&#034;' => '"', '&#x22;' => '"');
    $double_preg = array('/&#0*34;/' => '&#034;', '/&#x0*22;/i' => '&#x22;');
    $others = array('&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&');
    $others_preg = array('/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;');
    if ($quote_style === ENT_QUOTES) {
        $translation = array_merge($single, $double, $others);
        $translation_preg = array_merge($single_preg, $double_preg, $others_preg);
    } elseif ($quote_style === ENT_COMPAT || $quote_style === 'double') {
        $translation = array_merge($double, $others);
        $translation_preg = array_merge($double_preg, $others_preg);
    } elseif ($quote_style === 'single') {
        $translation = array_merge($single, $others);
        $translation_preg = array_merge($single_preg, $others_preg);
    } elseif ($quote_style === ENT_NOQUOTES) {
        $translation = $others;
        $translation_preg = $others_preg;
    }
    // Remove zero padding on numeric entities
    $string = preg_replace(array_keys($translation_preg), array_values($translation_preg), $string);
    // Replace characters according to translation table
    return strtr($string, $translation);
}