How in-place editing finds the right ACF field, block or widget

A developer's explanation of how W-Pencil marks values as WordPress renders a page, gives every ACF row, block and Elementor widget an address and saves changes back safely. It includes the theme API for values a theme prints itself.

W-Pencil team 16 min read

It marks every value at the moment WordPress prints it. W-Pencil wraps each editable value, inside the filter that outputs it, in a pair of invisible Unicode characters carrying a key, and the key points to a descriptor such as acf/42/en/cards_1_title/0/value that says exactly where the value is stored. When the editor saves, the server resolves that descriptor again and checks the person's rights and a version hash before it writes through ACF, the block markup or Elementor, as one revision.

This article follows one value from render to save, for developers who build themes. It matches W-Pencil 1.0 and its How markers work documentation.

Why is mapping the page back to storage hard?

By the time HTML reaches the browser, nothing says where "Our services" came from. On a typical agency site, the same heading could be:

  • a paragraph block, stored as HTML between <!-- wp:paragraph --> comments in post_content;
  • a sub-field in the second row of an ACF repeater, stored as post meta named cards_1_title (field name, row index, sub-field name: the pattern ACF's query documentation matches with locations_%_city);
  • a setting of an Elementor widget, stored with the rest of the page's layout as JSON in post meta, as Elementor's data structure documentation describes.

There are three ways an editor can recover the link:

Approach How it works Where it breaks
Attributes on elements The theme adds a data- attribute or a class to each editable element Every template needs changes, and values without an element of their own, like a phrase inside a sentence or an alt attribute, can't carry one
Matching text Find the stored value whose text matches what was clicked The same words appear in many places: "Learn more" may be on a page a dozen times
Markers in the value Wrap the value itself, where it's printed, in invisible characters that carry a key Anything that computes from the string sees the characters, so they must be kept out of URLs, attributes and the head

W-Pencil uses markers. The rest of this article is how it keeps their downside in check.

Step 1: where are values marked?

Each integration marks the values it owns through the filters WordPress, ACF and Elementor already run, so a theme that prints values the WordPress way needs no changes.

Value Usually printed by Marked in
The page's title the_title() in the main loop, the Post Title block the_title, render_block_core/post-title
A hand-written excerpt the_excerpt() get_the_excerpt
The site's title and tagline bloginfo(), the Site Title and Site Tagline blocks option_blogname, option_blogdescription
Classic menu items wp_nav_menu(), with any walker wp_setup_nav_menu_item
Blocks the_content, template parts, synced patterns render_block_data and render_block, which track each block's position
ACF fields get_field(), the_field(), get_sub_field() acf/format_value
Elementor widgets Elementor's renderer elementor/frontend/widget/before_render and after_render
Image alt text wp_get_attachment_image() and the functions built on it wp_get_attachment_image_attributes

Two consequences follow. An ACF value read unformatted, like get_field( 'title', false, false ), skips acf/format_value and isn't marked. And only values that belong to the page are marked: its own post (or term, on a term archive) and content the whole site shares, like options pages, menus and template parts. Lists of other posts are left alone on purpose.

Who gets markers?

W-Pencil decides once per request, at template_redirect. Markers are added only for:

  • a logged-in user with the Edit on the page capability, w_pencil_use;
  • a GET request for a front-end page: not wp-admin, admin-ajax, the REST API, WP-Cron, a feed, an embed, robots.txt, a sitemap, a preview or the Customizer;
  • not a page builder's own editor or preview;
  • and only values the person may edit, by WordPress's own checks, such as edit_post for a page and its fields or edit_theme_options for menus and template parts.

Everyone else gets clean HTML, and so do WP-CLI and cron. Editors' pages are sent with nocache_headers() and DONOTCACHEPAGE, so a page cache never stores a marked page or serves one to visitors.

Step 2: what does a marker look like?

A marker pair is made of characters that render as nothing:

  • Open: U+2060 (word joiner), then the target's number in base 3, written with U+200B, U+200C and U+200D as the digits 0, 1 and 2, then U+2060 again.
  • Close: U+2060 U+2060.

The number is the first 8 hex digits of the descriptor's SHA-256, so it's deterministic: cached HTML and the table of targets always agree. In PHP, the open marker for a descriptor is:

PHP
$descriptor = 'acf/42/en/cards_1_title/0/value';
$number     = (int) hexdec( substr( hash( 'sha256', $descriptor ), 0, 8 ) );
$digits     = '';
do {
	$digits = array( "\u{200B}", "\u{200C}", "\u{200D}" )[ $number % 3 ] . $digits;
	$number = intdiv( $number, 3 );
} while ( $number > 0 );

$open  = "\u{2060}" . $digits . "\u{2060}";
$close = "\u{2060}\u{2060}";

The table that goes with the markers is printed with the editor's settings as wPencilSettings.wPencil.t. Its key is k followed by the number in base 36, and each entry holds the descriptor and the label the popup shows:

JSON
{
  "k13tpucy": { "d": "acf/42/en/cards_1_title/0/value", "l": "Title" }
}

Escaping functions such as esc_html() leave these characters alone, so a theme's own escaping keeps working. For rich text, the markers go just inside the first and last text, so wpautop() never gives a marker a paragraph of its own.

What stays unmarked?

W-Pencil keeps markers out wherever a value leaves the page as something other than text. The <head> is buffered and cleaned, so the <title>, meta tags and JSON-LD never carry them. sanitize_title(), esc_url() and wp_trim_words() drop them, and wptexturize() treats one as a space, so quotes still curl the right way. Values that would break if marked, such as a web address, an email address, a number or a slug like hello-world, are never marked.

Step 3: what is a descriptor?

A descriptor is six parts, type/id/lang/field/delta/prop: what holds the value, its ID, the language the page shows (from WPML or Polylang, or und), the field, the item and the part, which is value, link or image. The browser only reads the part. The server works out everything else when a popup opens, and checks it again on every save.

Type Example What it addresses
post post/42/en/post_title/0/value A post's title, excerpt or featured image
site site/0/und/blogname/0/value The site's title or tagline
menu_item, menu menu_item/311/und/title/0/link A classic menu item, or a menu's name
block block/42/en/0.2.1~core.paragraph~content/0/value An attribute of a stored block
acf acf/42/en/sections_2_heading/0/value An ACF field on a post, a term or an options page
acfb acfb/42/en/5~lab.hero~items_0_title/0/value A field of an ACF Block
el el/42/en/3f9a2c1~title/0/value A setting of an Elementor widget

ACF fields and repeater rows

An ACF value is addressed by the name it's stored under, so the second row's title in a cards repeater is cards_1_title. A term's fields use term_12 as the ID, and options pages use options, or options_fr with a multilingual plugin.

Rows are positional, so the version of a row's value starts with 8 hex digits of the structure above it (the rows and layouts), followed by 8 of the value. If someone added, removed or reordered rows after the page loaded, the save is refused with "This field is no longer here, or its rows changed. Reload the page."

The save itself goes through ACF, as the edit screen does: ACF validates the value (required fields, character limits, your acf/validate_value filters) and acf/save_post runs. ACF's values are also copied into the page's new revision, so Restore This Revision brings them back.

Gutenberg blocks

Blocks stored in post_content have no stable ID, so they're addressed by position. In block/42/en/0.2.1~core.paragraph~content/0/value:

  • 42 is the post that owns the blocks: the page, a synced pattern's wp_block post, a template part or a wp_navigation post;
  • 0.2.1 is the path through the stored blocks, with freeform HTML between blocks left out;
  • core.paragraph is the block's name, with . for /;
  • content is the attribute.

The version is 8 hex digits of the post's block layout and 8 of the value. A save whose layout half is stale is refused as gone rather than treated as a conflict, because the path may now point at a different block.

A save rewrites only the bytes that hold the value, such as one span of the block's HTML or one attribute of its image. W-Pencil finds rich text the way WordPress's block bindings find an attribute's HTML, and an image's attributes with the HTML Tag Processor. Every other character of the post stays as it was, which is why the block editor opens the page afterwards with no "This block contains unexpected or invalid content" warning. Finding one block's bytes in the stored content uses WP_Block_Processor, which WordPress added in 6.9, so blocks are edited on the page from WordPress 6.9.

Some blocks are skipped on purpose, such as anything inside a Query Loop and text that holds a shortcode. The Gutenberg blocks doc lists them.

ACF Blocks

An ACF Block's fields live in the block's own data, and ACF's block ID changes with that data, so it can't be an address. The descriptor combines both worlds: acfb/42/en/5~lab.hero~items_0_title/0/value is the block's path, its type (lab/hero, with . for /) and the field's storage name. While the page renders, the Gutenberg mode's block tracker answers the w_pencil_current_block filter with the block being rendered, which is how the ACF mode knows where a field belongs. Without the Gutenberg mode, ACF Block fields aren't marked.

Elementor widgets

Elementor gives every element an ID that survives moving it, so W-Pencil uses it. el/42/en/3f9a2c1~title/0/value is the element's ID and the setting. An item of a repeater control, like the second line of an Icon List, is found by the item's own _id: 3f9a2c1~icon_list.a1b2c3d~text. Because the IDs are stable, a widget that was moved since the page loaded is still found.

Only controls on a widget's Content tab can be addressed. Settings on the Style and Advanced tabs, settings whose names start with _ (Elementor's own), values filled by dynamic tags and controls hidden by the widget's conditions never get a pencil. Elementor 4's atomic widgets aren't marked in W-Pencil 1.0.

A save goes through Elementor, as its editor's Update does, so Elementor's own filtering, cache clearing and revisions apply. The Elementor doc has the details.

Step 4: what does the browser do with the markers?

W-Pencil's script is printed in wp_footer at priority 1, before the theme's own scripts. It runs first. It removes every marker from the page's text and attributes and binds the text between each pair to its target.

  • Text: the run between a pair gets the outline and the pencil.
  • Attributes: a marker in alt makes the image editable, even when the alt text is empty. On an element with no text of its own, such as an icon link, a marked aria-label or title gets the pencil.
  • Copies: markers inside an element with data-w-pencil-ignore, such as a carousel's cloned slides, are removed without a pencil.
  • Content added later: a theme that moves marked markup into the page after load hands the new element over:
JavaScript
document.querySelector('.modal-body').append(template.content.cloneNode(true));
if (window.wPencilPlatform) {
  window.wPencilPlatform.attach(document.querySelector('.modal-body'));
}

The toolbar and popups live in a shadow root, so the theme's CSS can't reach them. Visitors load none of this.

Step 5: what happens when the editor saves?

The server never trusts what the browser drew. In order, a save:

  1. Resolves each item from its descriptor, up to 20 in one save. An item that's gone, or that the person may not edit, stops it.
  2. Holds back pages someone has open in the block editor or Elementor (wp_check_post_lock()): the popup says "Being edited in the backend".
  3. Locks each root, such as the post, the option or the menu item, in sorted order so two saves never deadlock. It waits up to 10 seconds.
  4. Reads fresh and compares versions with the ones the popup opened on.
  5. Cleans every value the way WordPress, ACF or Elementor would in the backend. For people without unfiltered_html, markup the filters would strip is refused rather than silently lost.
  6. Writes everything in one transaction, with one revision per post. The revision's note, kept in the _w_pencil_log revision meta, reads like "Edited on the page: Home › Cards #2 › Title".
  7. Remembers the save so an Undo straight after it can put it back, free.

When something doesn't fit, the answer says why, and nothing is written:

Answer Why What the editor sees
409 busy Another save holds the lock "Someone else is saving this page right now. Try again in a moment."
409 conflict The value changed since the popup opened "This content changed while you were editing", with both versions and Use latest version or Overwrite with my version
410 gone The item, or the blocks or rows around it, changed "This content no longer exists, or the page changed. Reload the page."
unsupported The value holds markup only unfiltered_html may save "Contains content only an administrator can save."

Undo, revisions and conflicts shows each message.

How do you make values your theme prints itself editable?

A value is marked when it passes through the filter its mode watches. A template that reads it straight from the database gets the plain value, and no pencil:

PHP
<h1><?php the_title(); ?></h1>                        <!-- marked -->
<h1><?php echo esc_html( $post->post_title ); ?></h1> <!-- not marked -->

When a template has to read a value another way, hand it to w_pencil() with the field it came from:

PHP
// An ACF field of the current post, read raw.
echo esc_html( w_pencil( get_field( 'hero_title', false, false ), array( 'field' => 'hero_title' ) ) );

// A post's title, read from the post object.
echo esc_html( w_pencil( $post->post_title, array( 'type' => 'post', 'id' => $post->ID, 'field' => 'post_title' ) ) );

The second argument is the descriptor's parts, or a whole descriptor string: field (required), type (acf when left out), id (the current post), delta (0), prop (value, link or image) and lang (worked out from the post). A third argument sets the popup's label. The tag returns the value unchanged for visitors, for anything the person may not edit and for a target no integration can resolve. A value that's already marked comes back as it is, so double-marking does no harm. HTML, such as a WYSIWYG field's, is marked as rich text and opens in the full editor.

With Timber, the same is a Twig filter, registered for Timber 1 and 2:

Twig
<h1>{{ post.raw_meta('hero_title')|w_pencil({ field: 'hero_title' }) }}</h1>

Use either only for values the page shows as they're stored. A value the template cut, joined or reformatted can't be saved back, so leave it unmarked.

Keep a plain copy for anything computed

Anything a template computes from a marked value sees the markers too: a length, a split, initials, an id or a data- attribute. Compute from the plain value instead:

PHP
// The title for an id, without markers.
$plain = get_post_field( 'post_title', get_the_ID(), 'raw' );
echo '<h2 id="' . esc_attr( sanitize_title( $plain ) ) . '">' . get_the_title() . '</h2>';

Never print a marked value into an address, an id, a class, a data- attribute, JSON or a form's default value. The browser cleans the page for editors, but those values are read before it does, or somewhere it never looks.

Other hooks for themes

  • w_pencil_active returns false to switch editing off on a page, such as one a plugin renders into a PDF.
  • w_pencil_display returns the HTML the page shows after a save, when the theme prints a value with extra processing, so the patched page matches the next reload.
  • w_pencil_integrations adds your own integration, a class implementing WPencil\Integrations\Integration, for a plugin's own values.

Hooks and the toolbar has examples of each.

What are the limits?

Some things can't be edited on the page:

  • Text written into templates isn't stored anywhere WordPress can save it. Move it into a block, a field or an option.
  • Empty text prints nothing to point at, so it's never marked. Image alt text is the exception.
  • Cut values: a value shortened by substr() or a theme's own trimming loses its close marker and gets no pencil. wp_trim_words() is safe, because it drops the markers.
  • Content fetched later through admin-ajax or the REST API is never marked: those requests stay clean on purpose.
  • Caches that ignore logged-in cookies serve editors the visitors' copy, with no toolbar.
  • Positional addresses for blocks and rows mean that when someone restructures a page while another person edits it, the second save asks for a reload. W-Pencil chooses that over guessing which block was meant.

Open in the backend in every popup still goes to the right screen, and the comparison of front-end editing options covers the alternatives.

Try it on a local site

Local development sites, such as *.ddev.site, *.test and *.local, are free and need no connection, so you can inspect the markers on your own theme without an account. On a live site, W-Pencil starts with 50 free edits, no account or card. Download W-Pencil and read Install and connect.

Questions and answers

Do W-Pencil's invisible markers affect SEO or visitors?

No. Markers are only added for logged-in people who may edit on the page, and even for them the head (the title, meta tags and JSON-LD) is cleaned. Visitors, crawlers, feeds and sitemaps get clean HTML, and editors' pages are sent with no-cache headers so caching plugins don't store them.

How does in-place editing save into the right ACF repeater row?

W-Pencil addresses a sub-field by the name ACF stores it under, such as cards_1_title for the second row's title. Its version includes a hash of the rows above it, so if rows were added, removed or reordered after the page loaded, the save is refused and the editor reloads instead of writing to the wrong row.

What happens when two people edit the same value?

Each save is compared with the version the popup opened on. If someone saved first, nothing is overwritten: the editor sees both versions and chooses Use latest version or Overwrite with my version.

Can I make a value my theme reads raw editable?

Yes, with the w_pencil() template tag or the |w_pencil Twig filter, naming the field it came from. For visitors, and for anything the person may not edit, it returns the value unchanged.

Why does a block save sometimes ask me to reload the page?

Blocks are addressed by their position in the stored content. If someone added, removed or reordered blocks since your page loaded, that position may now point at another block, so W-Pencil refuses the save rather than guess.

Can my own plugin's content get pencils?

Yes. A class that implements W-Pencil's Integration interface, added with the w_pencil_integrations filter, marks its values while the page renders and finds, checks, reads and writes them on save. Gutenberg, ACF and Elementor support are built the same way.