Hooks and the toolbar
What a theme needs for W-Pencil, the filters for what the page shows after a save and where editing is on, values a theme prints itself, and the toolbar's CSS custom properties.
8 min read WordPress 6.6+ Updated 2 Dec 2026
What a theme needs
Most themes need nothing. W-Pencil marks values through WordPress's own functions and filters, and prints its editor in the footer. Check three things:
- The theme calls
wp_footer(). The editor's script and its table of targets are printed there, at priority 1, before the theme's own footer scripts. Block themes always do. Without it, editors get no toolbar. - Values are printed the WordPress way:
the_title(),the_excerpt(),the_post_thumbnail(),bloginfo(),wp_nav_menu(), blocks, ACF'sget_field()andthe_field(). How markers work lists what each marks. - A page cache skips logged-in people. Editors' pages are sent with
nocache_headers()andDONOTCACHEPAGE. A cache that ignores both must still pass requests with awordpress_logged_in_cookie through to WordPress, or editors get the visitors' copy, with no toolbar.
What the page shows after a save
After a save, W-Pencil patches the page with the new value without a reload. It uses the HTML WordPress would print: the stored value escaped, run through the block's own render, or through ACF's or Elementor's formatting. If your theme prints a value with extra processing, return the same HTML from the w_pencil_display filter, so the patch matches the next reload.
For example, a theme that prints an ACF heading without its outer <p>:
add_filter(
'w_pencil_display',
function ( string $html, string $value, string $target, string $kind ): string {
if ( str_starts_with( $target, 'acf/' ) && str_contains( $target, '/hero_heading/' ) ) {
$html = preg_replace( '#^<p>(.*)</p>$#s', '$1', trim( $html ) );
}
return $html;
},
10,
4
);$htmlis the HTML the page shows.$valueis the stored value.$targetis the target, likepost/42/en/post_title/0/valueoracf/42/en/hero_heading/0/value.$kindis the editor:plain,textarea,inline,rich,linkorimage.
It runs when a popup opens and when a save answers, for text values. Links and images are patched from their own fields, the text and address or the image and its alt text.
Switch editing off on a page
w_pencil_active decides whether the current page is edited on the page at all. Return false for pages whose HTML is stored or sent somewhere else, such as a page a plugin renders into a PDF or a cache of its own:
add_filter(
'w_pencil_active',
function ( bool $active ): bool {
return $active && ! is_page( 'print-version' );
}
);It runs once per request, at template_redirect, only for people with Edit on the page. With false, the page has no markers and no toolbar, as for visitors.
Values your theme prints itself
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:
<h1><?php the_title(); ?></h1> // marked
<h1><?php echo esc_html( $post->post_title ); ?></h1> // not markedPrint with WordPress's functions and the value gets its pencil. The same goes for Timber and other template engines: menus and ACF fields read through ACF's own functions are marked as they are in PHP templates. The page's title is marked where the main loop prints it, with the_title() inside while ( have_posts() ), or by the Post Title block.
The template tag and the Twig filter
When a template has to read a value some other way, hand it to w_pencil() with the field it came from. For visitors, and for anything the editor may not change, it returns the value as it is.
// 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 parts of a descriptor (or a whole descriptor string, as in How markers work): 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 label the popup shows; the field's own label is used when it's empty. 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:
<h1>{{ post.raw_meta('hero_title')|w_pencil({ field: 'hero_title' }) }}</h1>A value that is already marked comes back as it is, so passing an ACF value through it as well does no harm. Use them only for values the page shows as they are stored. A value the template has changed (cut, joined or reformatted) can't be saved back, so leave it unmarked.
Keep a plain copy
Markers are characters in the string. Anything a template computes from a marked value sees them too: a length, a split, initials, an id or a data- attribute. W-Pencil already cleans slugs, URLs, trimmed words and <head>. For everything else, compute from the plain value, which no filter marks:
// 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>';
// An ACF value without ACF's formatting, so without markers.
$count = (int) get_field( 'items_count', false, false );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 sees.
Known limits
- Text written in templates isn't stored anywhere WordPress can save it, so it can't be edited on the page. Move it into a block, a field or an option first.
- Empty text prints nothing to point at, so an empty field is never marked. Images are the exception: their alt text is marked even when empty.
- Cut values: a value cut after marking, by
substr()or a theme's own trimming, loses its close marker and gets no pencil.wp_trim_words()is safe: it drops the markers. - Effects that run once, such as split-text animations, show the typed text in the live preview. They come back on the next page load.
Content added after the page loads
W-Pencil binds the page once, when its script runs in the footer. Values marked after that, such as a widget printed later in the footer, are sent at the very end of wp_footer and bound then.
If your theme's script moves marked markup into the page later, for example from a <template> element into a modal, hand the new element to W-Pencil once it's in the document:
document.querySelector('.modal-body').append(template.content.cloneNode(true));
if (window.wPencilPlatform) {
window.wPencilPlatform.attach(document.querySelector('.modal-body'));
}wPencilPlatform only exists for editors, so the check keeps visitors' pages working. Content fetched with admin-ajax or the REST API is never marked: those requests are left clean on purpose.
The toolbar
The toolbar sits where the settings page puts it: at the bottom or the top of the window, left, centre or right, at a distance from the edge you can set there too. At the top it sits below WordPress's admin bar. Your theme can move it and recolour the Editing on switch with four custom properties on :root, which win over the settings page's distance:
:root {
--w-pencil-toolbar-bottom: 72px; /* above the site's own bottom bar */
--w-pencil-toolbar-top: 96px; /* with the toolbar at the top: below a sticky header */
--w-pencil-toggle-bg: #1a1917;
--w-pencil-toggle-ink: #f5f0e6;
}--w-pencil-toolbar-bottom: the gap under a toolbar at the bottom. The default is 16px, or 8px on screens up to 520px wide. Messages sit 56px above it.--w-pencil-toolbar-top: the gap above a toolbar at the top, under the admin bar. The same defaults.--w-pencil-toggle-bgand--w-pencil-toggle-ink: the switch's background and text, near-black and white by default. When editing is off it turns light grey, whatever you set.
These four are the only ones the toolbar reads from your theme. Keep your theme's own custom properties off the --w-pencil- prefix, so none reach the editor by accident.
Side and colours are settings. The toolbar's side, the pencil's corner and the outline and pencil colours are under W-Pencil › Editing, not in CSS.
Plugins with their own values
A plugin that stores content of its own can make it editable on the page with an integration of its own: a class that implements WPencil\Integrations\Integration, added with the w_pencil_integrations filter. An integration marks its values while the page renders, and finds, checks, reads, writes and redisplays them when the browser asks. The interface's file in the plugin, src/Integrations/interface-integration.php, documents each method. Gutenberg, ACF and Elementor are built the same way, and the Integrations tab lists every one W-Pencil found.
add_filter(
'w_pencil_integrations',
function ( array $integrations ): array {
$integrations['events'] = new My_Events_Integration();
return $integrations;
}
);For Elementor widgets whose text and address belong together, the w_pencil_elementor_link_pairs filter says which settings make one link. Elementor has the details.