method_code_languages

 
 
 
 

apply_filters( 'method_code_languages', array $languages )

Filters the languages the Code block knows — the stored value and the label its pill shows.


Description

method_code_get_languages() returns the value => label map behind the Code block’s Language option, and this filter runs over it. Server-side the list does two things: it supplies the pill label when a block has no label of its own, and it sanitises the stored language — a value that is not in the list (after the aliases) renders as plain text. Highlighting is a separate matter: the grammars live in lib/blocks/utils/highlight.js, so a value added here highlights only once its grammar is registered there and the block rebuilt. Changing a label is the practical use.


Parameters

$languages (array) :
Stored value ⇒ label. Default: php PHP, php-template PHP + HTML, html, css, scss, javascript JavaScript, typescript TypeScript, json, bash Shell, markdown, plaintext Plain text.


Source

File: lib/blocks/method-code/method-code.php, function method_code_get_languages()

PHP
return apply_filters( 'method_code_languages', $languages );

Usage

PHP
add_filter( 'method_code_languages', function ( array $languages ) {
    $languages['bash'] = 'Terminal';
    return $languages;
} );

Notes

  • Runs on every render and in the editor’s option list, so keep the callback cheap.
  • Removing a key makes blocks stored with it render as plain text; the aliases (zsh, sh → bash and so on) are resolved before the list is consulted and are not filterable.
  • The editor shows the same labels through getLanguageLabel(), which reads the localised list.

Related

  • Code block — the block and its storage contract

Changelog

VersionDescription
2.0.0-beta30Introduced, with the Code block (a64c771).