7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. Creative Coding

Making the WordPress Editor Look Pretty Using CodeMirror

Read Time: 6 mins

WordPress contains two built-in editors that allow you to edit theme files directly from within the browser, they the Theme Editor and Plugin Editor. By default, the editor looks very simple, and hard to edit because the colors are totally black and grey. In this tutorial, I will show you how to integrate CodeMirror.js into the editors to make the look prettier. Our code snippets will be highlighted with more color, the editors will even have line numbers, and more.

Step 1 Preparing

First of all, you need to download the CodeMirror library. It is a powerful library, and supports many programming languages and features. After downloading and extracting, let's place this directory into your theme's directory (I use Twenty Eleven as a demo) and name it codemirror.

Then, we create a file called codemirror.php inside this directory and include this file in the functions.php file of your theme.

Open our codemirror.php and move to the next step.

"CodeMirror is a JavaScript component that provides a code editor in the browser. When a mode is available for the language you are coding in, it will color your code, and optionally help with indentation."

Step 2 Register Scripts and Styles

Above snippets will hook the codemirror_register function into two hooks, load-theme-editor.php and load-plugin-editor.php. It means when the Theme Editor or Plugin Editor is requested, they will call our codemirror_register function.

Core Library

Inside this function, we register all needed script and style files. codemirror.js and codemirror.css are two main components of the CodeMirror library, they are in the lib directory.


blackboard.css is a theme file, the theme directory in the CodeMirror library also has many other theme files, you can change depending on your preferences. There are more available themes, see them here:

  • Default:
  • Blackboard:


Modes are JavaScript programs that help color (and optionally indent) text written in a given language. Their value is a MIME type value, these depend on the language we work with. You can see all modes in the mode directory within the CodeMirror library. Because WordPress' built-in Editor can edit HTML, PHP, CSS (more here), so I register theses scripts on the next lines of codemirror_register function. The reason why I've registered XML and Clike scripts will be explained later.

After registering all script files, we have to enqueue them every time our admin section loads. So I add the codemirror_enqueue_scripts function to the admin_enqueue_scripts hook. Then we also embed a control javascript file (we can modify, add or delete options script) of CodeMirror to admin head.

Step 3 Enqueue Scripts

The above function has no big issue to explain, it just enqueues all scripts that we registered earlier. Open the Theme Editor and view source and you will see these scripts were embedded in the header.

Step 4 Hook Up Script

Overview Usage

Basically, our script is:

This function will be embedded in the admin head section, CodeMirror will effect any element that contains the newcontent ID, because our textarea element that holds our code snippets has this ID.

When using CodeMirror without any options, CodeMirror will use its own default options. For more using more options, we can do so like this:

Those additional options will make our editor have line numbers and the blackboard theme. Notice in the mode option, this option will determine what mode CodeMirror should use (In step 2, we mentioned what mode is). Because each file type has its own mode, we have to define this option to make CodeMirror work fine and display more accurately. Below are some MIME type values:

  • "text/javascript" for JavaScript mode
  • "text/css" for CSS mode
  • "application/x-httpd-php" for PHP mode, this mode require XML, JavasScript, CSS and C-Like modes. That is the reason we must register XML and C-Like scripts above.

Simple HTML/PHP mode based on the C-like mode. Depends on PHP, XML, JavaScript, CSS, and C-like modes. (see here)

Advanced Usage

For best usage, we need to define what file type we're editing to use the correct mode. Our goal is to make the script use the right mode automatically. Here is a solution:

Every time we click on a file to edit it, the $_GET['file] variable will hold the path to that file. For example:

  • wp-admin/theme-editor.php?file=header.php
  • wp-admin/theme-editor.php?file=style.css
  • wp-admin/plugin-editor.php?file=akismet.php

And these code snippets will help us to define file extensions that we're editing:

Then depending on each extension, we assign a different MIME type value for that match.

Except for the first time we open the Theme or Plugin Editor, we don't click on any file to edit so the $_GET['file'] variable doesn't exist. WordPress will open a default file to edit automatically. For the Theme Editor, the default file is the style.css file and another PHP file in the Plugin Editor. So we need to define the MIME type value as "text/css" in the Theme Editor and "application/x-httpd-php" in the Plugin Editor.


Everything's done but the editor looks messy. We need to modify the CSS file a little. In the lib directory, we open the codemirror.css file, find below code snippets:

Replace with:

Your Turn

CodeMirror has many features, it is a really powerful javascript library. Read CodeMirror's manual for getting fully informed on the features available. You can find out the other features or how to customize them, and add to your WordPress code yourself. It's quite easy: register, enqueue and hook up the script, like the way I've done above.


In this tutorial, I don't focus too much on CodeMirror and its features, I just demonstrate the way to integrate it into the WordPress Editor to make our Editor look prettier or easy to work with. Hope this tutorial was useful for everybody.

If you think my tutorial has any problem, or you have better ideas, or something to add, leave your comments below! We would very much appreciate it.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Scroll to top
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.