The Code field type provides a syntax-highlighted code editor powered by CodeMirror. It supports multiple programming languages with features like line wrapping, syntax highlighting, and configurable themes.
array(
'type' => 'code',
'id' => 'example_code',
'label' => 'Custom CSS',
'language' => 'css',
'height' => 300,
'theme' => 'dark',
)For Default Field Properties, see Field Types Definition.
The programming language for syntax highlighting. Defaults to 'html'. Supported languages:
html(default)javascriptorjscssphpsqlmarkdownormdxmljson
The height of the code editor in pixels. Defaults to 200.
The color theme for the editor. Defaults to 'dark' (VS Code dark theme). Any other value uses the default light theme.
The field stores the code as a string in the database. The content is not processed or modified when saved.
array(
'type' => 'code',
'id' => 'custom_css',
'label' => 'Custom CSS',
'description' => 'Add custom CSS styles for this page.',
'language' => 'css',
'height' => 250,
'theme' => 'dark',
)array(
'type' => 'code',
'id' => 'tracking_script',
'label' => 'Tracking Script',
'description' => 'Add custom JavaScript for analytics tracking.',
'language' => 'javascript',
'height' => 300,
)// Get the code content from the meta field
$custom_css = get_post_meta( get_the_ID(), 'custom_css', true );
if ( ! empty( $custom_css ) ) {
echo '<style>' . wp_kses( $custom_css, array() ) . '</style>';
}
// Output a tracking script
$tracking_script = get_post_meta( get_the_ID(), 'tracking_script', true );
if ( ! empty( $tracking_script ) ) {
echo '<script>' . esc_js( $tracking_script ) . '</script>';
}array(
'type' => 'toggle',
'id' => 'enable_custom_css',
'label' => 'Custom CSS',
'title' => 'Add custom CSS to this page',
),
array(
'type' => 'code',
'id' => 'custom_css',
'label' => 'Custom CSS',
'description' => 'Enter CSS styles for this page.',
'language' => 'css',
'height' => 300,
'conditions' => array(
array( 'field' => 'enable_custom_css', 'value' => true ),
),
)$f = new \Wpify\CustomFields\FieldFactory();
$f->code(
label: 'Custom CSS',
language: 'css',
height: 300,
);- The code editor includes an error boundary that falls back to a simple textarea if the editor fails to load
- Code fields are well-suited for storing custom code snippets, templates, or configuration data
- The field does not provide code execution, validation, or sanitization beyond basic string handling
- For very large code blocks, consider increasing the height or providing external editing capabilities
- For rich text editing without syntax highlighting, use the
wysiwygfield type instead