wp_enqueue_code_editor( array $args ): array|false

Enqueues assets needed by the code editor for the given settings.

Description

See also

Parameters

$argsarrayrequired
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.

Return

array|false Settings for the enqueued code editor, or false if the editor was not enqueued.

Source

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.

Changelog

VersionDescription
4.9.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example of how to get code highlighting set on a custom settings textarea.

    function code_editor_enqueue_scripts() {
    	if ( 'my_settings_page' !== get_current_screen()->id ) {
    		return;
    	}
    
    	// Enqueue code editor and settings for manipulating HTML.
    	$settings = wp_enqueue_code_editor( array( 'type' => 'text/html' ) );
    
    	// Return if the editor was not enqueued.
    	if ( false === $settings ) {
    		return;
    	}
    
    	wp_add_inline_script(
    		'code-editor',
    		sprintf(
    			'jQuery( function() { wp.codeEditor.initialize( "my_textarea_id", %s ); } );',
    			wp_json_encode( $settings )
    		)
    	);
    }
    add_action( 'admin_enqueue_scripts', 'code_editor_enqueue_scripts' );

You must log in before being able to contribute a note or feedback.