wppaste
WordPress

wptexturize( string $text, bool $reset = false ): string

Since
0.71
Source
wp-includes/formatting.php:37
Replaces common plain text characters with formatted entities.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
24–279

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 401.

Plugin surface
3 hooks

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.

Called by
9

9 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • regexregular expression over the whole inputpreg_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.

WhenInstructionsCalls it makes
empty($text)24none
!empty($text) && $run_texturize !== false && !isset($wp_cockneyreplace) && $apos !== "'" && $value242–275apply_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 !== "'" && !$value246–279apply_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

PHPCompiledExecutedBranchesNotes
8.6-dev40024–278351 fewer instruction than PHP 8.5
8.540124–27935
8.440124–2793556 fewer instructions than PHP 8.3
8.345724–27635
8.245724–27635
8.145724–27635
7.445724–27635

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:

  1. apply_filters( run_wptexturize )filterline 78 (+41 into the body)

    Filters whether to skip running wptexturize().

  2. apply_filters( no_texturize_tags )filterline 220 (+183 into the body)

    Filters the list of HTML elements not to texturize.

  3. apply_filters( no_texturize_shortcodes )filterline 228 (+191 into the body)

    Filters the list of shortcodes not to texturize.

Uses · 10

Used by · 9

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( '&#8220;', 'opening curly double quote' );		/* translators: Closing curly double quote. */		$closing_quote = _x( '&#8221;', 'closing curly double quote' ); 		/* translators: Apostrophe, for example in 'cause or can't. */		$apos = _x( '&#8217;', 'apostrophe' ); 		/* translators: Prime, for example in 9' (nine feet). */		$prime = _x( '&#8242;', 'prime' );		/* translators: Double prime, for example in 9" (nine inches). */		$double_prime = _x( '&#8243;', 'double prime' ); 		/* translators: Opening curly single quote. */		$opening_single_quote = _x( '&#8216;', 'opening curly single quote' );		/* translators: Closing curly single quote. */		$closing_single_quote = _x( '&#8217;', 'closing curly single quote' ); 		/* translators: En dash. */		$en_dash = _x( '&#8211;', 'en dash' );		/* translators: Em dash. */		$em_dash = _x( '&#8212;', '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 write

Changelog

Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.