Advertisement
  1. Code
  2. Theme Development
Code

Adding a Custom CSS Editor to Your Theme Using ACE

by
Difficulty:IntermediateLength:ShortLanguages:

Whenever someone downloads your theme and decides that some of the CSS isn't working for them, they usually go into style.css and manually modify the stylesheet to their liking. This isn't too hard to do, but it does present an issue when the next release of your theme is available and they decide to update.

All of their changes will then be overwritten. A great work-around is to offer a Custom CSS editor so that all the changes they make will be safely stored in the database and keeping up to date in the future will not be an issue.


The ACE Editor

ACE Editor

Anyone familiar with using a desktop IDE such as Coda, Sublime and Kimodo will be right at home within the ACE editor's interface. ACE is a web-based code editor that can easily be embedded into any web page or online app. It features syntax highlighting for over 40 languages and also includes a live code checker to help improve your code as you write.

In this tutorial, I plan on guiding you through the steps needed to get ACE installed in your theme so you can offer this great feature to your users.


Step 1: Installing ACE

The ACE editor is hosted on GitHub, but if you take a look at what you get when you download the project, you might be overwhelmed by the number of files. There are really only three files that we need and you can find them in the download package I provided through the Download link above.

  • ace.js
  • mode-css.js
  • worker-css.js

These three files need to be included in a folder named "ace". Place this folder in the root of your theme.


Step 2: The Helper File

In order to insert ACE into your theme, you need to create a helper JavaScript file that will set everything up on your page to make the ACE editor work correctly. Create a new file called "custom-css.js" with the following code and add it to the root of your theme as well.

The code above inserts the ACE editor into the page and makes sure that the CSS your users enter is also entered into the textarea field so that it can be stored in the WordPress database. There is also a check in there for IE7, which is incompatible with ACE so it will just load the basic textarea.


Step 3: Enqueueing the Files

With all the appropriate JavaScript files added, you have to hook into admin_enqueue_scripts to make sure your scripts appear on your Custom CSS editor admin page.


Step 4: The Custom CSS Editor

Now you need to create your theme option page in the wp-admin where the Custom CSS editor will be displayed. To add a theme option page, you need to hook into the admin_menu action.

Add the following to functions.php:

You also need to register a Custom CSS settings field so you can store it in the WordPress database. To do that you need to hook into the admin_init action and use the register_settings() function.

Since you've already added the option page and registered the settings, you can now create the content of your page:

The only thing missing is some code validation. Since you're allowing the user to insert CSS, there isn't much validation you can do but it's still smart to have this little function in place:


Step 5: The Front End

Adding the Custom CSS editor to the wp-admin is now taken care of, but the CSS isn't being displayed on the front end, so it isn't actually doing anything other than being stored in the database. In order to actually display the CSS on the front end, you need to hook into the wp_head action.


Conclusion

Many of my customers find the Custom CSS editor to be one of the best features offered in my themes. Taking the time to go through this tutorial to learn how to set one up in your own theme will benefit anyone who uses it. It's an amazing tool to help users customize your theme without having to worry about compromising their changes when future updates are released.

If you have any comments or feedback on anything you read above, please feel free to discuss it below.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.