| 开发者 | jonhenshaw |
|---|---|
| 更新时间 | 2026年6月15日 08:28 |
| PHP版本: | 7.4 及以上 |
| WordPress版本: | 7.0 |
| 版权: | GPLv2 or later |
| 版权网址: | 版权信息 |
core/code block. In the editor
it adds a "Code language" dropdown to the block sidebar; on the front end
it highlights the code with Prism.js using a custom token palette, prints
the language name as a small label on the block, and pins a
copy-to-clipboard button to the top-right corner.
aria-label, a polite
status region that announces "Copied to clipboard," and a visible "✓"
state for two seconds after a successful copy. Falls back to
document.execCommand('copy') on non-HTTPS or older browsers.defer strategy so they
never block rendering.prefers-color-scheme automatically. Override from
Tools → Code Blocks by switching to Default — Always light /
Always dark, or by picking any of the static Prism themes.language block attribute on
core/code, which lives in the block delimiter comment rather than the
saved markup. That means blocks without a language stay valid and
existing content is never migrated.
On render, the plugin uses WP_HTML_Tag_Processor to add data-language
to the <pre> and language-xxx to the <code> server-side — so KSES
won't strip data-* attributes for non-admin authors, and there is no
block-validation churn.
Prism core and the per-language grammars are bundled under
assets/prism/ at v1.30.0 (MIT — see assets/prism/LICENSE.txt). They
register as deferred scripts with explicit dependency ordering (e.g.
markup-templating before php, clike before languages that extend
it). The copy-button script depends on the last grammar in the chain,
so all of Prism is present before the copy UI is wired up. Reading
code.textContent returns the original source even after Prism wraps
tokens in spans, so the copied text is unaffected by highlighting.
Self-hosting Prism (rather than loading from a public CDN) keeps the
third-party-script supply chain off the plugin's surface and means the
plugin works on sites with strict CSPs or no external egress.
code-block-enhancer folder to /wp-content/plugins/,
or upload the .zip via Plugins → Add New → Upload Plugin.Bash/Shell, CSS, HTML/Markup, JavaScript, JSON, PHP, Python, SQL, and YAML. The dropdown also includes "None (plain text)" to render a block without highlighting.
Open Tools → Code Blocks → Language packs and enable the pack that contains the language you need. The baseline 9 languages are always available; the other 40 grammars are grouped into packs you can toggle on. Anything you enable shows up in the Code block's "Code language" dropdown automatically — no code editing required.
No. The language is stored as a block attribute, not baked into the saved markup, so blocks without a language stay valid and the front-end language class is applied at render time. Existing content is not migrated and is unaffected.
No. The token CSS only enqueues on singular posts/pages where
has_block( 'core/code' ) is true, and the Prism scripts are only
enqueued from inside the render_block filter for core/code — so a
page with no code block ships none of these assets. Prism is also loaded
with the defer strategy so it never blocks rendering.
Nothing extra. Prism and the copy-button script are bundled with the
plugin and served from your own origin, so a script-src 'self' policy
is enough. There is no external CDN call from the front end.
Go to Tools → Code Blocks in WP Admin. The "Code block theme" dropdown lists every bundled theme in three groups: Coywolf (Default — Auto / Always light / Always dark), Prism (built-in), and Prism Themes (community). Below the dropdown a live preview pane re-renders a sample PHP snippet in the highlighted theme on every change — your site keeps the previously saved theme until you click Save Changes. The selected theme's stylesheet is enqueued only on posts that contain a code block; only one theme file is ever loaded per request.
If you're on the bundled Default — Auto theme (the first-install
default), switch to Default — Always light or
Default — Always dark in Tools → Code Blocks. Picking any of the Prism themes also locks the
appearance — those themes are static and don't react to OS dark mode.
The lock is implemented in CSS — there is no inline <style> injected
per request — so it composes cleanly with caching plugins.
The 8 stock themes are bundled from PrismJS/prism at v1.30.0 (MIT — see
assets/themes/LICENSE-prism.txt). The 37 community themes are bundled
from PrismJS/prism-themes (MIT — see
assets/themes/LICENSE-prism-themes.txt). They are served from your own
origin alongside the rest of the plugin's assets — no external request
at runtime.
The label only renders when a language is set (the CSS rule is
.wp-block-code[data-language]::before). If the block was created
before the plugin was installed, open it in the editor and pick a
language from the sidebar so the language attribute is saved.