How To Add Custom Editor Styles to Gutenberg

Standard

Adding Custom Gutenberg editor styles in WordPress is a nice touch to any theme, as it allows authors to get a basic preview of their content before it gets published. It seems like a complicated task, but it’s actually pretty simple, once you get started.

Before

Default Gutenberg Editor Styles

After

Custom Gutenberg Editor Styles

The PHP

It begins with registering and enqueueing a CSS file in your theme.

The CSS

The CSS is a bit trickier because there are so many blocks and elements to cover. Here is an example that I’ve put together for this demo that can be a starting place for you.

How to Add Full Width Images in Gutenberg

Theme Preview with a Full-Width Image
Standard

Full width images are a sweet new feature that Gutenberg offers to WordPress theme developers. In order to enable these in your theme, it takes just a few lines of code.

Problem #1

By default, the Gutenberg Image Block only has the three classic alignment options: left-aligned, centered, and right-aligned.

Gutenberg Default Image Alignment Options
Gutenberg Default Image Alignment Options

Enabling Wide Images in the Gutenberg Editor

First, a bit of PHP.  Add this to your theme’s functions.php, or wherever else you are adding your add_theme_support() functions.

Now, you can find two new icons on your Image Gutenberg Block, wide-align, and full-width.

Gutenberg Image Block Updated Options

When one of these options is selected, it will display correctly in the editor.  If you select wide-align, the image gets wider, and if you select full-width, the image fills the editor horizontally.

Wide Image Previews in the Gutenberg Editor

Gutenberg Image Block Wide Width in the Editor
A wide image in the Gutenberg Editor
Gutenberg Image Block Full Width in the Editor
A full-width image in the Gutenberg Editor

Problem #2

There’s just one issue: Although WordPress adds special classes to the image markup, this doesn’t affect your actual theme styles.

Gutenberg Image in the Theme, unstyled
An unstyled full-width image in the Theme.

The Solution

To solve this, add your own CSS.  Here’s a snippet I came up with:

A few notes about the CSS:

  • calc() tells the browser to calculate a value (usually) based on the screen size.
  • vw refers to “viewport width,” or screen width.

The Result

After the styles are applied:

Gutenberg Wide Image in the Theme
A wide-aligned image in the theme after adding the CSS.
A full-width image in the theme after adding the CSS.
A full-width image in the theme after adding the CSS.

So, with less than 20 lines of code, you can add this powerful and attractive feature to your themes.

Further Reading

Simple Custom CSS Version 4.0 Released

Standard

After nearly 2 years, I’m proud to announce that Simple Custom CSS has received a big upgrade!

Background

I was fortunate enough to work on the first iteration of the WP Customize Code Editor Control and associated “Additional CSS” Customizer section released in WP version 4.7.  It was a huge learning experience, and my first major contribution to WP Core.

I’ve taken the lessons learned during that release to bring you a completely updated version of Simple Custom CSS!

The Customizer

screenshot-2aI’ve updated Simple Custom CSS to now use the Customizer, allowing live previews of your CSS before you save any changes.

I’ve styled the WP_Customize_Code_Editor_Control to give a similiar experience to the Core “Additional CSS” section, meaning that the editor is full width/height so that you have more room to view your code than the default display of the edtor.  And, just like Core, this supports CSSLint.

The Settings Page

screenshot-1
The Simple Custom CSS Settings Page
CSS Linting in Simple Custom CSS
CSS Linting

The Settings page now supports CSSLint, which means that it will clearly point out syntax errors.  I’ve also modified the colors there to show more contrast, and to more closely match the core WP CodeMirror styles.

I’ve also updated the packaged CodeMirror to the latest versions.  These hadn’t been updated in nearly 4 years.

The Future

There are a few features I’d like to build out in the future, including support for Sass and LESS.  I have some ideas on how to do this thanks to Weston Ruter’s WP Custom CSS Demo Plugin.

Eventually, I’m going to remove the plugin’s packaged CodeMirror scripts.   WP core now includes the same CodeMirror scripts that have been used in the Simple Custom CSS settings page, but I’ve kept the plugin’s packaged CodeMirror JS just in case you’re running an older version of WP that doesn’t yet have CodeMirror support.

I also want to move the Customizer code to JS.  I’ve seen a lot of others use Simple Custom CSS as a base for their plugins and theme tools (which I think is great!) and I want to keep it in PHP for a while so other developers can better follow how I’ve set it all up.  I hope this will lead to more widespread adoption of the Code Editor Control.

Much further down the road, I want to remove the Settings page altogether.  I’d like to see wider adoption of the Customizer for settings, plus at this point for the plugin it has become redundant to maintain both editors.  When I do that, I’ll be sure to notify everyone in advance, and perhaps just point the “Appearance > Custom CSS” link to the Customizer Section directly.  I’d love your feedback on this.

Thank You!

sccss-v1
v1.0 of Simple Custom CSS

I built this plugin out of personal need back in August 2013, and at the time I had no idea how much of a help it would be to others in the WP Community.

It’s thanks to you that Simple Custom CSS has come so far!  Your feedback and support have been so encouraging to me over the years, and I love having a way to give back to the Community that has given me so much.

I hope this update will make the plugin even more useful to you and give you a more powerful editing experience.

Here’s to many more years of Simple Custom CSS!

Adding Support for Vertical Featured Images in WordPress Themes

Standard

Here’s a quick and easy way to add support for Vertical Featured images in your WordPress themes.

How it Works

Using a filter hook, we’re going to add a class to the Featured Image markup if the image’s height is greater than its width. Then, using CSS we’ll float it to the right. Pretty simple, huh?

The Script

In your functions.php file add:

The CSS

This code floats the image to the right and adds some margin to separate the image from the post’s content. Be sure to include it in your CSS file.

.vertical-image {
	float:right;
	margin-bottom: 1em;
	margin-left: 2%;
	max-width: 33%;
}

Caveats

Your Post title will need to be clear: none; if this happens:

Screen Shot 2014-01-02 at 2.13.46 PM

You may need to add overflow:hidden to your post’s CSS to prevent this from happening in case the post excerpt is too short:

Screen Shot 2014-01-02 at 1.59.06 PM

The Result

This is what it should look like if the Feature Image has a vertical (portrait) orientation:

Screen Shot 2014-01-02 at 1.53.14 PM

Conclusion

When researching this, I had a hard time finding resources on this topic, so I hope this helps! If you find this post helpful, be sure to pass it on, including a link to this post!

The First Step To Coding Like A Pro: Formatting

Standard

At its core, good formatting is more than putting a spit shine on your plugin or theme. Its something that is done from the moment you crack open that first blank text document and begin writing. Formatting is the practice of careful, conscious coding.

I interviewed for an awesome job last week. They were interested in me and wanted to see a code sample of my work. I didn’t have much that was ready for review (because I’ve been working in Front End Development for the last year), so I wrote a Plugin from scratch. I went 96 hours with only a couple of naps, working my hardest to crank out coding perfection. I threw in everything that I could think of: AJAX, OOP, Validation/Sanitization, Actions & Filters, the Settings API, even localization. When I was done, I proudly submitted it for review.

What was the reviewer’s response back to me? “Your code is sloppy.” In fact three-fourths of his comments related to tabs and spacing.

Why did he care so much about such small details? Once I took the time to think about it, the answer became obvious.

Why Formatting Matters

1. It makes your code better.

If your code is written to the correct standard, it has been written and reviewed with attention to detail. It takes several passes to get any file up to standard, and in the course of formatting, you’d be surprised how many bugs you can find. Reviewing your code means refining your code. To put it another way, I can’t imagine you’ll ever see beautiful code that throws lots of errors or breaks things.

Furthermore, organizing functions in a logical way helps you see new ways to write your program. It can show you places where you are repeating yourself, revealing good sections to turn into classes/methods. Don’t forget the DRY principle of programming: Don’t Repeat Yourself.

Good formatting includes commenting your code. It makes your work easy to navigate, allowing others to troubleshoot errors, as well as reminding you of whatever the heck you were doing during the late night coding binge when you wrote it. Well documented code is much easier to extend as well.

2. It makes you look better.

It requires a lot of discipline to write clean code, and it is quickly assumed that the author of organized, well-planned code is a person with an eye for detail. Adherence to coding standards demonstrates that you are well-versed in the language/platform you’re working with. Most of all, it shows that you care; if you’re going to put in the time to do excellent work, chances are you’ll do excellent work for a client or employer.

Principles of WordPress Formatting

So, what goes into good formatting? If we need to adhere to the standards, what are they?

First of all, go take a look at the WordPress Coding Standards Doc. There are four sections, one for PHP, HTML, CSS, and JavaScript. Take the time to review each document. Here are some highlights from the docs:

PHP Highlights

  • Indent your code, and use Tabs instead of Spaces
  • Include commas after the last items in arrays
  • No shorthand PHP tags
  • Read up on the section “Space Usage
  • Commenting/Documenting code. There’s a lot here about the PHPdoc format of commenting, the industry standard.

HTML in a Nutshell

CSS Notes

  • Each Selector should be on its own line
  • Use lowercase, hyphenated selectors (not camelCase or underscored)
  • Always end in semicolons (C’mon, folks, is it necessary to say this?)
  • Order Properties alphabetically

JavaScript Formatting

  • “Always put spaces on both sides of the opening and closing parenthesis of if, else if, for, forin, do, while, and switch blocks.”
  • Name functions and variables using camelCase, not underscores.
  • Use the var keyword as much as possible to keep variables out of the global scope
  • Never end an Object with a comma
  • jQuery and that pesky $

Best Practices

Some of these rules will be natural for you based on your background, but for those which aren’t, keep a list of important formatting points handy. Practice these standards in everything you write, no matter how seemingly insignificant, and they’ll become part of your personal style of coding.

Unless you’re still using NotePad, your IDE (text editor) should have some good tools to help you format your code correctly. Turn on syntax highlighting based on the language you’re using, and set your preferences to eliminate trailing spaces on save.

One of the best ways to clean up your code is to have it reviewed by others. Find some other WordPress geeks and review each other’s code. Be teachable. The process will be humbling, but it will grow you as a developer.

Conclusion

In the end, I got the job! When all was said and done, the reviewer was able to see past my amateur-ish-ness and see potential in my work. Good leaders acknowledge that this is a learning process for us all (In fact, in writing this post I’ve noticed a few things I need to go back and fix in my own code).

So what questions do you have about WordPress formatting? Do you have a horror story about ugly code? Leave a note in the comments!

Get specific with CSS by using body_class()

Standard

body_class() is a template tag that can be a very useful tool to target content on specific pages.

How it works

Adding it to a theme is pretty easy. In header.php it goes along with the <body> tag thusly:

<body <?php body_class(); ?> >

and it outputs something like this (depending on what page you are viewing):

<body class="home blog">

…and the output changes to indicate if you are on pages, posts, archives, etc.

How This is Useful

Let’s say the text on your site is black, but you want it to be blue on just the home page. By adding body_class() you can target your home page, and your CSS will look something like this:

body {
color: black;
}

body.home {
color: blue;
}

Of course, there are many other (more useful) applications for this tool. It can also help out when writing jQuery scripts that use CSS classes to perform actions.

Other Helps

Here is a list of possible classes output by body_class().

If you want to do the same thing but with posts instead of your whole body, you can use post_class().

body_class() is great for CSS and jQuery, but if you are trying to target your home page in PHP, you can use is_home()… but that’s information for another post.