Skip to content

Latest commit

 

History

History
70 lines (49 loc) · 1.25 KB

code.mdx

File metadata and controls

70 lines (49 loc) · 1.25 KB

Code

A CodeMirror-backed code editor with syntax highlighting.


PHP API

Field::code(string $key): CodeField

Type-specific methods

->language(string $lang): static

Programming language for syntax highlighting. Default: 'text'. Common values: 'php', 'javascript', 'css', 'html', 'json', 'bash'.

->rows(int $rows): static

Editor height in lines. Default: 10.

->wrapLines(bool $wrap = true): static

Wrap long lines. Default: false.


Storage

Stored as a string. Sanitized with wp_kses_post() (strips <script> and unsafe tags while preserving code characters like <, >, &).


Example

Field::code('custom_css')
    ->label('Custom CSS')
    ->language('css')
    ->rows(15),

Field::code('template_snippet')
    ->label('PHP Snippet')
    ->language('php')
    ->wrapLines(),

Template usage

To display code in a <pre> block:

<pre><code class="language-{{ 'php' }}">{{ ctrlfield_get('template_snippet') }}</code></pre>

To execute PHP stored in the field (use with extreme caution):

// Only do this if code comes from a trusted admin — never from public input
$code = ctrlfield_get('custom_script');
if ($code) {
    eval($code);
}