Code
Overview
A syntax-highlighted code sample. The sample is stored once, as plain text, and highlighted when the page renders — so it can be edited anywhere the block markup can, with nothing to regenerate.
- Block name:
method/code - Title: Code (Method)
- Category:
method-component-blocks - Supports:
classNameonly —html,anchorandalignare off - Render type: Dynamic (PHP
render_callback:method_code_render()) - Inner blocks: No
- Scripts:
editorScript(index.js) andviewScript(frontend.js); both bundle highlight.js from lib/blocks/utils/highlight.js - Source: lib/blocks/method-code/method-code.php, lib/blocks/method-code/src/edit.js, lib/blocks/method-code/src/frontend.js
Usage
Insert a Code block and type or paste into the canvas: the canvas is a live editor, not a preview — a transparent textarea sits over the highlighted <pre> with the same font metrics, so the caret lands on the colored text and every keystroke re-highlights. Tab inserts a tab and Enter keeps the current line’s indentation. Pick the Language in the block settings, and optionally give the pill a Pill text (a file name reads well; empty shows the language name), hide the pill or the copy button, or turn on Wrap long lines.
Because the block is dynamic, what is saved is a single self-closing delimiter holding the sample as an attribute:
<!-- wp:method/code {"code":"add_filter( 'method_code_languages', function ( $languages ) {\n $languages['bash'] = 'Terminal';\n return $languages;\n} );","label":"functions.php"} /-->
No highlighted HTML is stored. The attribute JSON is escaped the way the editor escapes every block’s attributes (-- becomes \u002d\u002d, <, > and & become \u003c, \u003e and \u0026, a quote inside the string becomes \u0022, and newlines are \n), so a sample can be changed through the REST API or a script by rewriting that one attribute. The code value is the text itself — not HTML-escaped — and is escaped on output.
Attributes at their default are omitted from the delimiter, as the editor omits them: a PHP sample with the pill and copy button on stores only code.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
code | string | "" | The sample, as plain text. A block whose sample is empty (or whitespace) renders nothing. |
language | string | "php" | One of the values method_code_get_languages() returns: php, php-template, html, css, scss, javascript, typescript, json, bash, markdown, plaintext. Aliases are accepted on render (zsh, sh, shell → bash; js, jsx → javascript; ts, tsx → typescript; md → markdown; xml, svg → html; text, txt → plaintext); anything else renders as plain text, never as highlight.js auto-detection. |
label | string | "" | Text for the header pill. Empty shows the language label (e.g. “PHP + HTML”). Tags are stripped. |
showLabel | boolean | true | Show the pill. |
showCopy | boolean | true | Show the copy button. With both this and showLabel off there is no header at all. |
wrapLines | boolean | false | Wrap long lines on the page instead of scrolling horizontally. The editor always wraps (so its two layers cannot scroll apart); this only affects the frontend. |
responsiveSettings | object | {"base":{"enabled":true},…} | Vertical margins per tier; see below. |
responsiveSettings Structure
Per-tier settings live under responsiveSettings.base, mobile, tablet and wide. A tier is only read while its enabled flag is true; base is always on. The keys this block’s CSS map reads:
margin.top,margin.bottom— the only spacing the block takes; the CSS map is#method-{id}→margin-top,margin-bottomin both the editor (cssMap) and PHP ($cssargs).
Control Sets
| Component | Panel | Breakpoints | Notes |
|---|---|---|---|
| Block panel: Code | Code | base | Language (SelectControl), Show language pill, Pill text (shown while the pill is on), Show copy button, Wrap long lines. |
MethodSpacingControls | Spacing | base + mobile / tablet / wide | include={['margin']} with top and bottom only. |
MethodResponsiveTabs | Mobile / Tablet / Ultrawide | mobile, tablet, wide | The per-tier margin controls. |
MethodStyleTag | — | all | Mirrors $cssargs in the canvas. |
Data & Dependencies
- highlight.js:
highlight.js(the first runtime dependency in lib/blocks/package.json), with only the grammars the block offers registered in lib/blocks/utils/highlight.js. The module exportsLANGUAGES,ALIASES,resolveLanguage(),getLanguageLabel(),highlightCode()andhighlightElement(), and is bundled into both the editor script and the view script so the canvas matches the page. - PHP mirror:
method_code_get_languages()(value → label; filterable, see Filters),method_code_get_language_aliases()andmethod_code_sanitize_language()keep the language list in step with the JS module. The pill label and the sanitisedlanguage-…class come from PHP; the grammar comes from JS. - View script (lib/blocks/method-code/src/frontend.js): highlights each
.method-code__codeand wires the copy button, which writes the<code>‘s text to the clipboard and shows “Copied” for two seconds. The async clipboard API needs a secure context, so on plainhttp://the button is removed rather than left to fail. Blocks added to the page later can be picked up withwindow.methodCode.init( container )or by dispatchingmethod:code-refreshondocument. - Styles: assets/scss/global.scss — the block’s colors are
--method-code-*custom properties on.method-code(background, foreground, header, pill, border, radius, padding, font, font size, line height, tab size, selection, and one per token: comment, keyword, string, number, function, variable, type, tag, attr, operator, meta, addition, deletion). The defaults are the Material Palenight palette. highlight.js only addshljs-*classes; it ships no CSS of its own here. - Responsive CSS:
method_get_block_responsive_styles()builds the margin rules, collected per instance bymethod_collect_css()under#method-{id}.
Frontend Rendering
<div class="wp-block-method-code method-code" id="method-68f1b2c3a4d5e" data-language="php">
<div class="method-code__header">
<span class="method-code__label">PHP</span>
<button type="button" class="method-code__copy" data-copy-label="Copy" data-copied-label="Copied" aria-label="Copy code to clipboard">
<svg class="method-code__copy-icon method-code__copy-icon--copy" …></svg>
<svg class="method-code__copy-icon method-code__copy-icon--copied" …></svg>
<span class="method-code__copy-text">Copy</span>
</button>
</div>
<pre class="method-code__pre" tabindex="0"><code class="method-code__code language-php">add_filter( 'method_code_languages', … );</code></pre>
</div>
The wrapper gains method-code--wrap when lines wrap and method-code--bare when there is no header. The language-… class on <code> is the one contract between PHP and the view script, which reads the language from it. Without JavaScript the sample is a readable, dark <pre>; with it, highlight.js tokenises the text in place.
Theming
A child theme recolors the block by overriding the custom properties — on :root for every block, or on .method-code:
.method-code {
--method-code-bg: #1e1e2e;
--method-code-fg: #cdd6f4;
--method-code-header-bg: #181825;
--method-code-pill-bg: #313244;
--method-code-keyword: #cba6f7;
--method-code-string: #a6e3a1;
--method-code-comment: #6c7086;
--method-code-font-size: 0.875rem;
}
Filters
| Hook | Type | Arguments | Description |
|---|---|---|---|
method_code_languages | Filter | array $languages | The value => label map the block knows. Server-side the list only supplies the pill label and sanitises the stored value — a value added here renders as plain text until its grammar is registered in lib/blocks/utils/highlight.js. Changing a label is the practical use. |
add_filter( 'method_code_languages', function ( array $languages ) {
$languages['bash'] = 'Terminal';
return $languages;
} );