Creates an HTML processor in the fragment parsing mode.
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. <h2>Current HTML Support</h2> <ul> <li>The only supported context is <body>, which is the default value.</li> <li>The only supported document encoding is UTF-8, which is the default value.</li> </ul>
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
static|null
The created processor if successful, otherwise null.
295publicstaticfunctioncreate_fragment($html,$context='<body>',$encoding='UTF-8'){296if('<body>'!==$context||'UTF-8'!==$encoding){297returnnull;298}299300$processor=newstatic($html,self::CONSTRUCTOR_UNLOCK_CODE);301$processor->state->context_node=array('BODY',array());302$processor->state->insertion_mode=WP_HTML_Processor_State::INSERTION_MODE_IN_BODY;303$processor->state->encoding=$encoding;304$processor->state->encoding_confidence='certain';305306// @todo Create "fake" bookmarks for non-existent but implied nodes.307$processor->bookmarks['root-node']=newWP_HTML_Span(0,0);308$processor->bookmarks['context-node']=newWP_HTML_Span(0,0);309310$root_node=newWP_HTML_Token(311'root-node',312'HTML',313false314);315316$processor->state->stack_of_open_elements->push($root_node);317318$context_node=newWP_HTML_Token(319'context-node',320$processor->state->context_node[0],321false322);323324$processor->context_node=$context_node;325$processor->breadcrumbs=array('HTML',$context_node->node_name);326327return$processor;328}
History
Introduced in 6.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
6.6.0
Returns static instead of self so it can create subclass instances.from the docblock
6.4.0
Introduced.from the docblock
About this page
Parsed data
Generated from the wordpress-develop 6.7.7 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.