WP_HTML_Processor::create_fragment( string $html, string $context = '<body>', string $encoding = 'UTF-8' ): static|null
- Since
- 6.4.0, 6.6.0
- Source
wp-includes/html-api/class-wp-html-processor.php:300
Description
Use this for cases where you are processing chunks of HTML that will be found within a bigger HTML document, such as rendered block output that exists within a post, the_content inside a rendered site layout.
Fragment parsing occurs within a context, which is an HTML element that the document will eventually be placed in. It becomes important when special elements have different rules than others, such as inside a TEXTAREA or a TITLE tag where things that look like tags are text, or inside a SCRIPT tag where things that look like HTML syntax are JS.
The context value should be a representation of the tag into which the HTML is found. For most cases this will be the body element. The HTML form is provided because a context element may have attributes that impact the parse, such as with a SCRIPT tag and its type attribute.
Current HTML Support
- The only supported context is
<body>, which is the default value. - The only supported document encoding is
UTF-8, which is the default value.
Compatibility
- WordPress
- since 6.6.0
- 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
$htmlstring- Input HTML fragment to process.
$contextstringoptional- Context element for the fragment, must be default of
<body>.Default:'<body>' $encodingstringoptional- Text encoding of the document; must be default of 'UTF-8'.Default:
'UTF-8'
Return value
static|null- The created processor if successful, otherwise null.
Performance profile
How much work a call to WP_HTML_Processor::create_fragment() 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
- 6–37
- Plugin surface
- None
- Called by
- 0
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 58.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below WP_HTML_Processor::create_fragment()
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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_HTML_Processor::create_fragment() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–8 | none |
$context === "<body>" && $encoding === "UTF-8" && is_string($html) && $context_processor === null | 18 | ::create_full_parser() |
$context === "<body>" && $encoding === "UTF-8" && !is_string($html) | 18 | __(), _doing_it_wrong() |
$context === "<body>" && $encoding === "UTF-8" && is_string($html) && $context_processor !== null && !->next_tag() && ->has_bookmark() && ->seek() | 32 | ::create_full_parser(), ->next_tag(), ->has_bookmark(), ->seek(), ->create_fragment_at_current_node() |
$context === "<body>" && $encoding === "UTF-8" && is_string($html) && $context_processor !== null && !->next_tag() && !->has_bookmark() | 33 | ::create_full_parser(), ->next_tag(), ->has_bookmark(), __(), _doing_it_wrong() |
$context === "<body>" && $encoding === "UTF-8" && is_string($html) && $context_processor !== null && !->next_tag() && ->has_bookmark() && !->seek() | 37 | ::create_full_parser(), ->next_tag(), ->has_bookmark(), ->seek(), __(), _doing_it_wrong() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 58 instructions, 6–37 executed per call, 8 branches. The work does not change between versions.
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.
Uses · 3
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- static::create_full_parser()
Source code
public static function create_fragment( $html, $context = '<body>', $encoding = 'UTF-8' ) { if ( '<body>' !== $context || 'UTF-8' !== $encoding ) { return null; } if ( ! is_string( $html ) ) { _doing_it_wrong( __METHOD__, __( 'The HTML parameter must be a string.' ), '6.9.0' ); return null; } $context_processor = static::create_full_parser( "<!DOCTYPE html>{$context}", $encoding ); if ( null === $context_processor ) { return null; } while ( $context_processor->next_tag() ) { if ( ! $context_processor->is_virtual() ) { $context_processor->set_bookmark( 'final_node' ); } } if ( ! $context_processor->has_bookmark( 'final_node' ) || ! $context_processor->seek( 'final_node' ) ) { _doing_it_wrong( __METHOD__, __( 'No valid context element was detected.' ), '6.8.0' ); return null; } return $context_processor->create_fragment_at_current_node( $html ); }Changelog
Introduced in 6.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
static instead of self so it can create subclass instances.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/html-api/class-wp-html-processor.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.