wptexturize( string $text, bool $reset = false ): string
- Since
- 0.71
- Source
wp-includes/formatting.php:37
Description
Returns given text with transformations of quotes into smart quotes, apostrophes, dashes, ellipses, the trademark symbol, and the multiplication symbol.
As an example,
'cause today's effort makes it worth tomorrow's "holiday" ... Becomes:
’cause today’s effort makes it worth tomorrow’s “holiday” … Code within certain HTML blocks are skipped.
Do not use this function before the 'init' action hook; everything will break.
Compatibility
- WordPress
- since 0.71
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$textstring- The text to be formatted.
$resetbooloptional- Set to true for unit testing. Translated patterns will reset.Default:
false
Return value
string- The string replaced with HTML entities.
Performance profile
How much work a call to wptexturize() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 24–279
- Plugin surface
- 3 hooks
- Called by
- 9
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 401.
Third-party callbacks on 'run_wptexturize', 'no_texturize_tags', 'no_texturize_shortcodes' run inside this call, and their cost is not bounded by anything here.
9 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - regexregular expression over the whole input
preg_match_all()called directly
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wptexturize() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($text) | 24 | none |
!empty($text) && $run_texturize !== false && !isset($wp_cockneyreplace) && $apos !== "'" && $value | 242–275 | apply_filters(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), explode(), _x(), explode(), array_merge(), …(), wp_spaces_regexp(), array_keys(), array_values(), array_keys(), array_values(), array_keys(), array_values(), apply_filters(), apply_filters(), preg_match_all(), array_keys(), array_intersect(), _get_wptexturize_split_regex(), preg_split() |
!empty($text) && $run_texturize !== false && !isset($wp_cockneyreplace) && $apos !== "'" && !$value | 246–279 | apply_filters(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), explode(), _x(), explode(), array_merge(), …(), wp_spaces_regexp(), array_keys(), array_values(), array_keys(), array_values(), array_keys(), array_values(), apply_filters(), apply_filters(), preg_match_all(), array_keys(), array_intersect(), _get_wptexturize_shortcode_regex(), _get_wptexturize_split_regex(), preg_split() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 400 | 24–278 | 35 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 401 | 24–279 | 35 | |
| 8.4 | 401 | 24–279 | 35 | 56 fewer instructions than PHP 8.3 |
| 8.3 | 457 | 24–276 | 35 | |
| 8.2 | 457 | 24–276 | 35 | |
| 8.1 | 457 | 24–276 | 35 | |
| 7.4 | 457 | 24–276 | 35 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 3
3 hooks fire while wptexturize() runs, in this order:
- apply_filters( run_wptexturize )filterline 78 (+41 into the body)
Filters whether to skip running wptexturize().
- apply_filters( no_texturize_tags )filterline 220 (+183 into the body)
Filters the list of HTML elements not to texturize.
- apply_filters( no_texturize_shortcodes )filterline 228 (+191 into the body)
Filters the list of shortcodes not to texturize.
Uses · 10
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _x()Retrieves translated string with gettext context.
- wp_spaces_regexp()Returns the regexp for common whitespace characters.
- _get_wptexturize_shortcode_regex()Retrieves the regular expression for shortcodes.
- _get_wptexturize_split_regex()Retrieves the combined regular expression for HTML and shortcodes.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- _wptexturize_pushpop_element()Searches for disabled element tags. Pushes element to stack on tag open and pops on tag close.
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wptexturize_primes()Implements a logic tree to determine whether or not "7'." represents seven feet, then converts the special char into either a prime char or a closing quote char.
Used by · 9
- WP_Theme::markup_header()Marks up a theme header.
- _get_plugin_data_markup_translate()Sanitizes plugin data, optionally adds markup, optionally translates.
- _wp_apply_block_content_filters()Applies standard content filters similar to the 'the_content' filter.
- _wp_menu_output()Display menu.
- gallery_shortcode()Builds the Gallery shortcode output.
- get_archives_link()Retrieves archive link content based on predefined or custom code.
- get_the_block_template_html()Returns the markup for the current template.
- hello_dolly_get_lyric()
- trackback_rdf()Generates and displays the RDF for the trackback information of current post.
Source code
function wptexturize( $text, $reset = false ) { global $wp_cockneyreplace, $shortcode_tags; static $static_characters = null, $static_replacements = null, $dynamic_characters = null, $dynamic_replacements = null, $default_no_texturize_tags = null, $default_no_texturize_shortcodes = null, $run_texturize = true, $apos = null, $prime = null, $double_prime = null, $opening_quote = null, $closing_quote = null, $opening_single_quote = null, $closing_single_quote = null, $open_q_flag = '<!--oq-->', $open_sq_flag = '<!--osq-->', $apos_flag = '<!--apos-->'; // If there's nothing to do, just stop. if ( empty( $text ) || false === $run_texturize ) { return $text; } // Set up static variables. Run once only. if ( $reset || ! isset( $static_characters ) ) { /** * Filters whether to skip running wptexturize(). * * Returning false from the filter will effectively short-circuit wptexturize() * and return the original text passed to the function instead. * * The filter runs only once, the first time wptexturize() is called. * * @since 4.0.0 * * @see wptexturize() * * @param bool $run_texturize Whether to short-circuit wptexturize(). */ $run_texturize = apply_filters( 'run_wptexturize', $run_texturize ); if ( false === $run_texturize ) { return $text; } /* translators: Opening curly double quote. */ $opening_quote = _x( '“', 'opening curly double quote' ); /* translators: Closing curly double quote. */ $closing_quote = _x( '”', 'closing curly double quote' ); /* translators: Apostrophe, for example in 'cause or can't. */ $apos = _x( '’', 'apostrophe' ); /* translators: Prime, for example in 9' (nine feet). */ $prime = _x( '′', 'prime' ); /* translators: Double prime, for example in 9" (nine inches). */ $double_prime = _x( '″', 'double prime' ); /* translators: Opening curly single quote. */ $opening_single_quote = _x( '‘', 'opening curly single quote' ); /* translators: Closing curly single quote. */ $closing_single_quote = _x( '’', 'closing curly single quote' ); /* translators: En dash. */ $en_dash = _x( '–', 'en dash' ); /* translators: Em dash. */ $em_dash = _x( '—', 'em dash' ); $default_no_texturize_tags = array( 'pre', 'code', 'kbd', 'style', 'script', 'tt' ); $default_no_texturize_shortcodes = array( 'code' ); // If a plugin has provided an autocorrect array, use it. if ( isset( $wp_cockneyreplace ) ) { $cockney = array_keys( $wp_cockneyreplace ); $cockneyreplace = array_values( $wp_cockneyreplace ); } else { /* * translators: This is a comma-separated list of words that defy the syntax of quotations in normal use, * for example... 'We do not have enough words yet'... is a typical quoted phrase. But when we writeChangelog
Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.