TUTORIAL: Using Sass and Compass for managing CSS in WordPress – Technosailor.com – Technosailor

I don’t often write tutorials but since the rebuild of the WP Engine website some months ago, I have been turned on to the use of a brilliant combination of tools made for development in a Ruby on Rails environment. That doesn’t mean we can’t make it work for WordPress too.

The tools are Compass combined with Sass (which means Sytactically Awesome Stylesheets or some random crap like that).
Sass is cool because it lets you do a whole bunch of stuff with CSS that you couldn’t normally. It’s a kind of abstraction layer above CSS which means you can write normal CSS if you want, but then why wouldn’t you just write normal CSS instead of using Sass?
I’m getting slightly ahead of myself, but With Sass you can do awesome things like variable/placeholders which is awesome for things like defining core elements of a stylesheet such as a palette of colors.
Then using those variables, you can just use these placeholders in your SCSS (Sassy CSS) file:
You can also write Mixins. Mixins are essentially reusable blocks of CSS that look a lot like functions you would see in languages like JavaScript, PHP or Ruby:
You can use that in your SCSS file as such:
But again, I’m getting ahead of myself.
When I talked about using Sass and Compass on Twitter, other WordPress devs made comments that made me want to write this tutorial. One person said that they were tired of Rails developers having all the toys – and it’s true. There are so many awesome things out there for Rails developers that we as PHP and WordPress developers don’t benefit from. This tutorial is non-exhaustive. There are tons of things that you can do with this that I won’t cover. But this should get you going with Mac OS X Lion, developing WordPress locally on your own machine and uploading to a theme file called “test” on a remote server.
First of all, you need to have a local version of WordPress running. There are a variety of ways to do this, but I use the approach of installing XAMPP. Here’s a tutorial on that. Follow the rabbit trail to configure Apache to look to your Sites directory, as I have, or make that translation in your head to what the default is. There’s plenty of instruction on how to do that on the internet.
For the specifics of this tutorial, it’s very important that you have the latest version of Ruby. The best way to do this is with a tool called rvm, or Ruby Version Manager. Install this with the following Terminal command:
Once rvm is installed, use it to install 1.9.3-p0, the only current version compatible with OS X Lion, and refresh your Terminal profile settings. The last command sets 1.9.3-p0 (or whatever future version you choose to use) as your default.
If you wish to verify that you’ve got the proper version of Ruby active, verify it with the ruby -v command.
My system reported:
Now, there are some gems that need to be installed. For developers unfamiliar with Ruby – or Rails – gems, are essentially additional libraries that are installed into the Ruby framework, much like PHP PEAR or PECL modules.
The first is Builder, which is used for building XML structures. The second one is Compass which will give us the ability to leverage Sass for CSS authoring.
Once we have everything installed properly, we can start a new project. I keep this out of my web directory (i.e. WordPress structure). Personally, I’ve created a sass directory under my user profile (/Users/aaron/sass) and run my projects out of it with a separate directory for each project.
Now we have to create our compass project. We do that with compass create from our ~/sass directory and then moving into the newly created directory.
Doing a directory listing should show something along these lines:
Good. Our project is created but we need to make some changes to make this work with WordPress. To do this, we need to edit the config.rb file which is the project configuration file. You can edit this file in Textmate, vi, or whatever you choose as your preferred text editor.
The default configuration is:
My config file looks like this:
Important changes here:
All configuration options can be referenced in the Compass docs.
Having modified the config file, we can take one of two approaches to generating the CSS in our WordPress theme. We can use the compass compile which will generate the files one time. Everytime modifications are made, however, this command would have to be re-run. I prefer, instead, to use compass watch which is a small process that remains running and watches your Sass project for changes and recompiles automatically when changes are made.
Simply run this command from inside the Sass project:
At this point, if you want to develop locally, all you have to do is have your WordPress style.css import these stylesheets.
Fortunately, with a little Ruby magic and some built in Compass hooks, we can also upload these newly created CSS files to our WordPress theme. In order to do this, you have to make sure the remote server has the theme directory created and if you are uploading to a subdirectory of that theme (e.g. theme_dir/css), that that directory is created as well.
In our case, the theme directory is test/ and I want to upload to a subdirectory test/css/.
Next we have to install two new gems – the Net::SSH and Net::SFTP gems. Installing these is as straightforward as the earlier gems:
Once this has done, include these in your config.rb file. I did this at the top which is best practice with Ruby.
Below all the previous configurations, add some configuration lines and replace values as needed for your own project:
Finally, we can leverage Compass’ built-in on_stylesheet_saved hook to upload to the remote server using your SFTP credentials:
Restart Compass, save one of your Sass .scss files with a change of some sort (from where CSS will be compiled), and watch your files be saved locally and remotely.
At this point, my full config.rb file looks like this:
Rails developers have all the fun. It’s true. But with Compass, Sass and a little bit of Ruby, PHP developers (including WordPress theme developers) have a great tool that will make workflows more efficient, CSS more readable and structured and central management more coherent.
Obviously, I did not get too far into the details of using Compass and Sass. That’s a whole tutorial to itself. For information on that, I’d recommend checking out these fine articles written on the topic:
(Protip: I love being able to nest CSS… try it)
Disclaimer: I am a PHP developer, not a Ruby developer. My Ruby code could probably be improved upon by someone who is more in tune with the Ruby language.
Aaron Brazell
Proudly powered by WordPress