Introducing the Enhanced Code Block: Syntax Highlighting and Extra

Introducing the Enhanced Code Block: Syntax Highlighting and Extra

Closing yr, WordPress.com presented new code editors for the block editor and the Further CSS enter field within the WordPress admin. This was once the primary level of a bigger effort to make enhancing code a extra stress-free revel in.

These days, I’m satisfied to announce the release of the second one level of that effort: introducing the brand new and advanced Code block.

This isn’t a brand new block. It’s an enhancement to the present Code block that you just’ve most likely already been the usage of, which incorporates a number of enhancements over the unique:

  • Syntax highlighting: Helps color-based syntax highlighting for over 100 not unusual languages.
  • Configuration: Make a decision to turn the filename, language identify, line numbers, or even come with a replica button for guests.
  • Drag-and-drop: Dragging a code report out of your laptop to the editor will mechanically turn out to be it to the Code block with the language set.
  • Transforms: Change into different code-supported blocks on WordPress.com, equivalent to Syntax Highlighter, to the brand new Code block.
  • Kinds: Customise syntax colours immediately from the editor or by way of ‘theme.json’ for those who’re a developer.

The use of the improved Code block

You shouldn’t have to allow anything else to start the usage of the brand new model of the Code block. It’s already to be had to make use of. Simply insert the Code block any place within the block editor and upload your code.

By way of default, when including a Code block and putting code, you’re going to see your code in Simple Textual content:

Example of code displayed in a blog post using plain text formatting.

After all, Simple Textual content doesn’t come with any syntax highlighting because it’s no longer a language. To modify this, make a choice a code language from the Settings → Language dropdown within the sidebar:

Example of the Code block displaying code using a language preset.

Syntax highlighting will then be carried out in response to the language that you just decided on, making the code a lot more readable for each you and your guests!

Professional tip: When you kind 3 backticks adopted by way of the language identify (e.g., ```php) after which hit Input, the editor will mechanically create a brand new Code block example and auto-fill the Language atmosphere.

If you wish to kick your Code block’s options up a notch, you’ll be able to additionally configure a number of different settings but even so the language:

  • Filename: Upload a customized filename to show within the best left of the code block (helpful when strolling readers via tutorials).
  • Display language identify: Presentations the language identify within the best proper nook of the block.
  • Display replica button: Inserts a Reproduction button within the best proper of the block, permitting web page guests to duplicate everything of the code.
  • Display line numbers: Presentations line numbers subsequent on your code at the left.
  • Line numbers get started at: Make a choice a beginning line quantity.

This will likely make your web page’s code examples a lot more reader pleasant:

Example of the Code block displaying code using the additional language settings.

Customizing the Code block colours

There are more than one techniques to customise the syntax highlighting and colours proven with the improved Code block. On this phase, I’ll stroll you via every from the fastest/most simple to the extra complicated ways.

Settling on a block taste

The Code block ships with 4 block types out of the field:

  • Default: Will use the default types and colours out of your theme.
  • No Spotlight: Disables syntax highlighting.
  • Solarized Gentle: A gentle shade scheme.
  • Solarized Darkish: A dismal shade scheme.

Topics too can check in further types. Settling on this type of types is the fastest approach to alternate how your Code block is output:

Customizing colours from the editor

You’ll be able to additionally customise the colours immediately from the editor by way of the Kinds → Colour panel within the block sidebar. The block has an intensive array of shade choices for customizing each facet of the syntax highlighting:

Selecting custom colors for the Code Block syntax formatting.

You aren’t restricted to just colours both. You’ll be able to customise any of the opposite to be had types, equivalent to Typography, Border, and extra. Those choices haven’t modified with the newest enhancement.

Customizing the Code block by way of theme.json

When you’re a developer or theme creator, you’ll in all probability need to outline default syntax colours and different types for the default output of the block. theme.json reinforce is integrated with this batch of improvements.

Right here’s what my customized Code block types appear to be after a couple of tweaks in theme.json:

Example of the Code Block using custom colors.

For the reason that WordPress device itself doesn’t reinforce customized colours by way of theme.json, the builders at WordPress.com in-built customized reinforce for this option.

You’ll be able to customise any of the syntax colours by way of settings.customized.core/code in theme.json. That is an object the place every secret’s the syntax shade identify and the worth is the colour itself.

Right here’s an instance snippet that you’ll be able to use to customise your individual colours:

Any legitimate CSS shade is supported, so that you’re no longer restricted to hex shade codes. Use CSS customized homes, RGBA, and extra.

If you wish to borrow my complete theme.json customizations, replica and paste the next code. It comprises further customized types to make the Code block even nicer:

Get started sharing code now.

Whether or not you’re publishing snippets or full-blown tutorials, the improved Code block makes sharing and styling code in WordPress.com smoother and extra customizable than ever earlier than. 

Syntax highlighting, block types, and customized shade choices put you in complete regulate of ways your code seems. 

With those enhancements, you’ll be able to focal point much less on formatting and extra on writing nice content material that is helping your readers be told and construct.

WPBeginner Highlight 27: WordPress 7.1, WPVibe AI All over, and a New Option to Report Your Display
WPBeginner Highlight 27: WordPress 7.1, WPVibe AI All over, and a New Option to Report Your Display by in Blog

August used to be a large month for WordPress. WordPress 7 ...

01 Sep, 2026 7  Person Liked it

WordPress.com Changelog: A New Web hosting Dashboard, a ChatGPT Plugin, and WordPress 7.1
WordPress.com Changelog: A New Web hosting Dashboard, a ChatGPT Plugin, and WordPress 7.1 by in Blog

August 14 – 27, 2026 Welcome again to the WordPr ...

30 Aug, 2026 7  Person Liked it

Your WordPress.com Website Now Works from Within ChatGPT
Your WordPress.com Website Now Works from Within ChatGPT by in Blog

You'll now set up your WordPress.com web page from Ch ...

27 Aug, 2026 6  Person Liked it

Offer Ends Tonight 12 PM

Lifetime Membership with Unlimited Access