wppaste
WordPress

WP_HTML_Processor::set_bookmark( string $bookmark_name ): bool

Since
6.4.0
Source
wp-includes/html-api/class-wp-html-processor.php:5750
Sets a bookmark in the HTML document.

Description

Bookmarks represent specific places or tokens in the HTML document, such as a tag opener or closer. When applying edits to a document, such as setting an attribute, the text offsets of that token may shift; the bookmark is kept updated with those shifts and remains stable unless the entire span of text in which the token sits is removed.

Release bookmarks when they are no longer needed.

Example:

<main><h2>Surprising fact you may not know!</h2></main>
 ^ ^
 \-|-- this H2 opener bookmark tracks the token

<main class="clickbait"><h2>Surprising fact you may no…
 ^ ^
 \-|-- it shifts with edits

Bookmarks provide the ability to seek to a previously-scanned place in the HTML document. This avoids the need to re-scan the entire document.

Example:

<ul><li>One</li><li>Two</li><li>Three</li></ul>
 ^^^^
 want to note this last item

$p = new WP_HTML_Tag_Processor( $html );
$in_list = false;
while ( $p->next_tag( array( 'tag_closers' => $in_list ? 'visit' : 'skip' ) ) ) {
 if ( 'UL' === $p->get_tag() ) {
 if ( $p->is_tag_closer() ) {
 $in_list = false;
 $p->set_bookmark( 'resume' );
 if ( $p->seek( 'last-li' ) ) {
 $p->add_class( 'last-li' );
 }
 $p->seek( 'resume' );
 $p->release_bookmark( 'last-li' );
 $p->release_bookmark( 'resume' );
 } else {
 $in_list = true;
 }
 }

 if ( 'LI' === $p->get_tag() ) {
 $p->set_bookmark( 'last-li' );
 }
}

Bookmarks intentionally hide the internal string offsets to which they refer. They are maintained internally as updates are applied to the HTML document and therefore retain their "position" - the location to which they originally pointed. The inability to use bookmarks with functions like substr is therefore intentional to guard against accidentally breaking the HTML.

Because bookmarks allocate memory and require processing for every applied update, they are limited and require a name. They should not be created with programmatically-made names, such as "li_{$index}" with some loop. As a general rule they should only be created with string-literal names like "start-of-section" or "last-paragraph".

Bookmarks are a powerful tool to enable complicated behavior.
Consider double-checking that you need this tool if you are reaching for it, as inappropriate use could lead to broken HTML structure or unwanted processing overhead.

Bookmarks cannot be set on tokens that do no appear in the original HTML text. For example, the HTML <table><td> stops at tags TABLE, TBODY, TR, and TD. The TBODY and TR tags do not appear in the original HTML and cannot be used as bookmarks.

Compatibility

WordPress
since 6.4.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

$bookmark_namestring
Identifies this particular bookmark.

Return value

bool
Whether the bookmark was successfully created.

Performance profile

How much work a call to WP_HTML_Processor::set_bookmark() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
10–13

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_HTML_Processor::set_bookmark()

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 · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_HTML_Processor::set_bookmark() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!->is_virtual()10->is_virtual(), ::set_bookmark()
->is_virtual()13->is_virtual(), __(), _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: 19 instructions, 10–13 executed per call, 1 branch. 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 · 4

Source code

	public function set_bookmark( $bookmark_name ): bool {		if ( $this->is_virtual() ) {			_doing_it_wrong(				__METHOD__,				__( 'Cannot set bookmarks on tokens that do no appear in the original HTML text.' ),				'6.8.0'			);			return false;		}		return parent::set_bookmark( "_{$bookmark_name}" );	}

Changelog

Introduced in 6.4.0. 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 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.