wp_enqueue_code_editor()
Enqueue assets needed by the code editor for the given settings.
Hooks from the function
Return
Array|false
. Settings for the enqueued code editor, or false if the editor was not enqueued.
Usage
wp_enqueue_code_editor( $args );
- $args(array) (required)
Args.
-
type(string)
The MIME type of the file to be edited. -
file(string)
Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to $type param. -
theme(WP_Theme)
Theme being edited when on the theme file editor. -
plugin(string)
Plugin being edited when on the plugin file editor. -
codemirror(array)
Additional CodeMirror setting overrides. -
csslint(array)
CSSLint rule overrides. -
jshint(array)
JSHint rule overrides. - htmlhint(array)
HTMLHint rule overrides.
-
Examples
#1 Example of creating a code editor for textarea
To do this, let's turn the Biography field on the profile edit page into an HTML code editor.
To get a better idea of what the code does, let's look at the HTML code of the textarea, which will eventually become a code editor:
<textarea id="description" name="description" rows="5" cols="30"></textarea>
add_action( 'admin_enqueue_scripts', function() { if ( 'profile' !== get_current_screen()->id ) { return; } // connect the code editor for HTML. $settings = wp_enqueue_code_editor( array( 'type' => 'text/html' ) ); // do nothing if CodeMirror is disabled. if ( false === $settings ) { return; } // initialization wp_add_inline_script( 'code-editor', sprintf( 'jQuery( function() { wp.codeEditor.initialize( "description", %s ); } );', wp_json_encode( $settings ) ) ); } );
The result is:

The variable $settings contains such an array:
Notes
Changelog
Since 4.9.0 | Introduced. |