On this page
wp_enqueue_code_editor( array $args ): array|false
Enqueues assets needed by the code editor for the given settings.
Description
See also
Parameters
$argsarray Required-
Args.
typestringThe MIME type of the file to be edited.filestringFilename to be edited. Extension is used to sniff the type. Can be supplied as alternative to$typeparam.themeWP_ThemeTheme being edited when on the theme file editor.pluginstringPlugin being edited when on the plugin file editor.codemirrorarrayAdditional CodeMirror setting overrides.csslintarrayCSSLint rule overrides.jshintarrayJSHint rule overrides.htmlhintarrayHTMLHint rule overrides.
Return
array|false Settings for the enqueued code editor, or false if the editor was not enqueued.
Source
File: wp-includes/general-template.php. View all references
function wp_enqueue_code_editor( $args ) {
if ( is_user_logged_in() && 'false' === wp_get_current_user()->syntax_highlighting ) {
return false;
}
$settings = wp_get_code_editor_settings( $args );
if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
return false;
}
wp_enqueue_script( 'code-editor' );
wp_enqueue_style( 'code-editor' );
if ( isset( $settings['codemirror']['mode'] ) ) {
$mode = $settings['codemirror']['mode'];
if ( is_string( $mode ) ) {
$mode = array(
'name' => $mode,
);
}
if ( ! empty( $settings['codemirror']['lint'] ) ) {
switch ( $mode['name'] ) {
case 'css':
case 'text/css':
case 'text/x-scss':
case 'text/x-less':
wp_enqueue_script( 'csslint' );
break;
case 'htmlmixed':
case 'text/html':
case 'php':
case 'application/x-httpd-php':
case 'text/x-php':
wp_enqueue_script( 'htmlhint' );
wp_enqueue_script( 'csslint' );
wp_enqueue_script( 'jshint' );
if ( ! current_user_can( 'unfiltered_html' ) ) {
wp_enqueue_script( 'htmlhint-kses' );
}
break;
case 'javascript':
case 'application/ecmascript':
case 'application/json':
case 'application/javascript':
case 'application/ld+json':
case 'text/typescript':
case 'application/typescript':
wp_enqueue_script( 'jshint' );
wp_enqueue_script( 'jsonlint' );
break;
}
}
}
wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
/**
* Fires when scripts and styles are enqueued for the code editor.
*
* @since 4.9.0
*
* @param array $settings Settings for the enqueued code editor.
*/
do_action( 'wp_enqueue_code_editor', $settings );
return $settings;
}
Hooks
- do_action( 'wp_enqueue_code_editor',
array $settings ) -
Fires when scripts and styles are enqueued for the code editor.
Related
Uses
| Uses | Description |
|---|---|
| wp_get_code_editor_settings() wp-includes/general-template.php | Generates and returns code editor settings. |
| wp_add_inline_script() wp-includes/functions.wp-scripts.php | Adds extra code to a registered script. |
| wp_get_current_user() wp-includes/pluggable.php | Retrieves the current user object. |
| wp_enqueue_script() wp-includes/functions.wp-scripts.php | Enqueue a script. |
| wp_enqueue_style() wp-includes/functions.wp-styles.php | Enqueue a CSS stylesheet. |
| wp_json_encode() wp-includes/functions.php | Encodes a variable into JSON, with some sanity checks. |
| current_user_can() wp-includes/capabilities.php | Returns whether the current user has the specified capability. |
| is_user_logged_in() wp-includes/pluggable.php | Determines whether the current visitor is a logged in user. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
Used By
| Used By | Description |
|---|---|
| WP_Widget_Custom_HTML::enqueue_admin_scripts() wp-includes/widgets/class-wp-widget-custom-html.php | Loads the required scripts and styles for the widget control. |
| WP_Customize_Code_Editor_Control::enqueue() wp-includes/customize/class-wp-customize-code-editor-control.php | Enqueue control related scripts/styles. |
Changelog
| Version | Description |
|---|---|
| 4.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_enqueue_code_editor