Tag Archives: CSS

How to Add JavaScript and CSS to Gutenberg Blocks the Right Way in Plugins and Themes

How to Add JavaScript and CSS to Gutenberg Blocks the Right Way in Plugins and Themes

icon-256x256While working on my Gutenberg Editor Development Course I had a simple question early on: “How do I enqueue my block JavaScript and CSS to work with the Gutenberg Editor?

The short answer is there are two ways:

  • enqueue_block_editor_assets – For enqueueing JavaScript and CSS in the admin editor only.
  • enqueue_block_assets – Enqueues on both the frontend of the site and in the admin editor (unless !is_admin() is used then it just enqueues on the frontend only).

Enqueueing Block JavaScript the Right Way

In general, you will want to load your main block JavaScript using enqueue_block_editor_assets since your main block JavaScript code needs to execute inside the editor.

Once a block is saved as content, the editor JavaScript is no longer needed, so most JavaScript for building blocks only needs to execute in the editor.

On occasion your block may require JavaScript to run properly on the frontend.  For example, if you were building a slideshow or a form or some other element that needed frontend interaction.  In this case, you would break out that functionality from your main block JavaScript and load it separately using enqueue_block_assets.

Enqueuing Block CSS the Right Way

The general principal for styling blocks in the new WordPress editor is to make them look the same in the editor as they do on the frontend.  To accomplish this, we enqueue our main block styles using enqueue_block_assets.

There are some instances where you need to style blocks in the editor different from the frontend.  Some reasons for this are to help give users visual cues for what they can edit or there may be controls in the editor that need styling and do no appear on the frontend.  If there are ever styles you want applied only in the editor, you can break these into a separate CSS file and load it using enqueue_block_editor_assets.

On the chance that you have styles that should only be applied to the frontend and never loaded in the editor, you can load them using enqueue_block_assets and wrap your wp_enqueue_style call inside of a !is_admin() conditional statement.

Example of Enqueueing Block JavaScript and CSS in a Plugin

This example below would go in a main plugin file that needs to load blocks.  See how both enqueue_block_assets and enqueue_block_editor_assets are used.

Some notes on the example above:

  • We have a file /assets/js/blocks.js with our main block JS code.  This is being loaded with enqueue_block_editor_assets into the editor.
  • We have a file /assets/css/blocks.editor.css with our editor only styles.  This is also being loaded in the same function call as our JavaScript, which hooks into enqueue_block_editor_assets.
  • Then we have a file /assets/js/blocks.shared.js with additional JS code that needs to be loaded on the frontend and backend.  This is being loaded with enqueue_block_assets into the editor and frontend.  Note, you may or may not need JavaScript loaded on the frontend depending on the type of blocks you build.
  • We also have /assets/css/blocks.styles.css, which is our main CSS file.  This will load on the front and backend since we have it hooked into enqueue_block_assets.
  • The final file /assets/css/blocks.frontend.css is wrapped inside of the !is_admin() conditional statement so it will not load in the editor as is usually the case with enqueue_block_assets. Note, you probably don’t need a frontend only stylesheet.  It is also recommended to use editor only styles to override shared frontend/editor styles rather than this approach demonstrated here for example purposes.

This should cover the basic use cases you will have for loading block JavaScript and CSS into your plugins.  Let me know if I missed something you find useful!

Example of Enqueueing Block CSS in a Theme

In general, you should NOT build blocks inside of themes.  Custom blocks should generally be built inside of plugins.  However, it is perfectly acceptable for themes to customize block styles, both in the editor and on the frontend.

The example below shows how to load CSS in your themes to override default block styles.  This code would go inside of  or be included into a theme’s functions.php file.

Some notes on the code above:

  • We are not including any block JavaScript since that should generally go in a plugin.
  • We have a file /assets/css/blocks.editor.css with our editor only styles.  This is being loaded using enqueue_block_editor_assets.  Note, there is a chance you may not need editor only CSS.
  • We also have /assets/css/blocks.styles.css, which is our main CSS file.  This will load on the front and backend since we have it hooked into enqueue_block_assets.
  • The final file /assets/css/blocks.frontend.css is wrapped inside of the !is_admin() conditional statement so it will not load in the editor as is usually the case with enqueue_block_assets. Note, you will likely not need a frontend only stylesheet.  It is also recommended to use editor only styles to override shared frontend/editor styles rather than this approach demonstrated here for example purposes.

A Note on File Organization and Compilation

The examples above show including only a few final JavaScript and CSS files all within ‘/assets/js’ and ‘/assets/css’.  It is likely that you will break your JavaScript and CSS into smaller files and compile them using a tool like webpack.

If you bundle your final JS and CSS into a different directory structure than the ‘/assets/js’ and ‘/assets/css’ structure used above, you may need to make some changes to the examples.  Same goes for how you name your files.

The examples above do not offer insight into how you should organize your modular JS and CSS or how you would setup a tool like webpack to handle compiling it all.  I do have some thoughts on these matters and you can see my course below or wait for upcoming blog posts on these topics.  However, in general, try to follow modern best practices.

To Learn More About Developing with the New WordPress Editor

If you would like to learn more about developing blocks with WordPress, please check out my course Gutenberg Development.

How to Deregister, Unqueue and Unhook a Parent Theme CSS Stylesheet

There comes a time when working with a WordPress project where you want to not include certain CSS from a parent theme or plugin.  For example, a parent theme may include extra stylesheets that you don’t want or a plugin may be adding styles that conflict with your site.

On a recent project, I wanted to remove the parent styles just on a certain section of the site.  Here’s how to do it.

First what you have to do is find the handle for the parent stylesheet you no longer want.  Let’s say for example that we had a twentyfifteen child theme and wanted to remove the parent styling.

You would go to the parent theme’s functions.php file and find the hook.  It can help to search for “wp_enqueue_style” to find the CSS includes faster.  Here is what it looks like in twentyfifteen:

TwentyFifteen Theme Functions.php Including Stylesheet

We can see the handle is “twentyfifteen-style” and this is what we will now use to dehook it using the code below:

function unhook_parent_style() {

  wp_dequeue_style( 'twentyfifteen-style' );
  wp_deregister_style( 'twentyfifteen-style' );

}
add_action( 'wp_enqueue_scripts', 'unhook_parent_style', 20 );

It’s important that we add this code into a function and then hook the function into wp_enqueue_scripts.

If you only want to unhook the styles on certain pages, you can also add conditional code like this:

function unhook_parent_style() {

  is_page_template( 'custom-template.php' ) {
    wp_dequeue_style( 'twentyfifteen-style' );
    wp_deregister_style( 'twentyfifteen-style' );
  }

}
add_action( 'wp_enqueue_scripts', 'unhook_parent_style', 20 );

This will only unhook the parent styles when WordPress is loading pages that use the custom-template.php file. You can also use any of these conditional statements as well.

Shout out to Brad Dalton over at WP Sites for the original code snippet on How To Deregister & Dequeue Style Sheets!

Adding CSS to WordPress Theme Via functions.php File

One of the incorrect way theme developers add their CSS to their theme is via the bloginfo(‘stylesheet_url’); tag in the header.php. This is a bad idea.  A better way to do this is via the functions.php file, much in the same way you properly link to JavaScript files a WordPress theme.

Here is the code for how to do this:

I’m a fan of the Flexslider Slideshow Plugin from WooThemes, so that’s what I’m setting up to link to just on the homepage.

The required stye.css and a custom CSS stylesheet inside of a css directory in the themes folder are being queued up inside the theme_styles() function and then loaded to execute and output into the header via the wp_head() function that should be included in the header.php file before the closing of the <head> tag.

For another read on this, check out this TutsPlus post on the topic.

style.css Comment Code for Twenty Twelve Theme

/*
Theme Name: Twenty Twelve
Theme URI: http://wordpress.org/extend/themes/twentytwelve
Author: the WordPress team
Author URI: http://wordpress.org/
Description: The 2012 theme for WordPress is a fully responsive theme that looks great on any device. Features include a front page template with its own widgets, an optional display font, styling for post formats on both index and single views, and an optional no-sidebar page template. Make it yours with a custom menu, header image, and background.
Version: 1.1
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: light, gray, white, one-column, two-columns, right-sidebar, flexible-width, custom-background, custom-header, custom-menu, editor-style, featured-images, flexible-header, full-width-template, microformats, post-formats, rtl-language-support, sticky-post, theme-options, translation-ready
Text Domain: twentytwelve

This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/