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.


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:

HTML
<!-- 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

AttributeTypeDefaultDescription
codestring""The sample, as plain text. A block whose sample is empty (or whitespace) renders nothing.
languagestring"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.
labelstring""Text for the header pill. Empty shows the language label (e.g. “PHP + HTML”). Tags are stripped.
showLabelbooleantrueShow the pill.
showCopybooleantrueShow the copy button. With both this and showLabel off there is no header at all.
wrapLinesbooleanfalseWrap 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.
responsiveSettingsobject{"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-bottom in both the editor (cssMap) and PHP ($cssargs).

Control Sets

ComponentPanelBreakpointsNotes
Block panel: CodeCodebaseLanguage (SelectControl), Show language pill, Pill text (shown while the pill is on), Show copy button, Wrap long lines.
MethodSpacingControlsSpacingbase + mobile / tablet / wideinclude={['margin']} with top and bottom only.
MethodResponsiveTabsMobile / Tablet / Ultrawidemobile, tablet, wideThe per-tier margin controls.
MethodStyleTag—allMirrors $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 exports LANGUAGES, ALIASES, resolveLanguage(), getLanguageLabel(), highlightCode() and highlightElement(), 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() and method_code_sanitize_language() keep the language list in step with the JS module. The pill label and the sanitised language-… class come from PHP; the grammar comes from JS.
  • View script (lib/blocks/method-code/src/frontend.js): highlights each .method-code__code and 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 plain http:// the button is removed rather than left to fail. Blocks added to the page later can be picked up with window.methodCode.init( container ) or by dispatching method:code-refresh on document.
  • 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 adds hljs-* classes; it ships no CSS of its own here.
  • Responsive CSS: method_get_block_responsive_styles() builds the margin rules, collected per instance by method_collect_css() under #method-{id}.

Frontend Rendering

HTML
<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:

child-theme/assets/scss/style.scss
.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

HookTypeArgumentsDescription
method_code_languagesFilterarray $languagesThe 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.
PHP
add_filter( 'method_code_languages', function ( array $languages ) {
    $languages['bash'] = 'Terminal';
    return $languages;
} );

Changelog

VersionDescription
2.0.0-beta30Introduced (a64c771).
2.0.0-beta31Editor icon redrawn with heavier chevrons; nothing else about the block changed (cd6f63f).