Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
DevOps

Add Bootstrap to a WordPress theme the right way

Load Bootstrap 5.3 in a WordPress child theme: local files or a CDN with SRI, deferred scripts, editor styles, Bootstrap menu classes and a lean Sass build.

CloudSat teamNovember 18, 20267 min read
Abstract illustration of a twelve-column grid of glowing tiles snapping into the outline of a web page
On this pageDecide whether Bootstrap belongs in the theme

Bootstrap gives you a grid, ready-made components and a large set of utility classes under the MIT license. WordPress gives you themes. The two work well together when the theme is built around Bootstrap, and badly when Bootstrap is dropped on top of a theme that already has its own design system, because two sets of base styles then fight over every heading and button. The current stable release is 5.3.9. Bootstrap 6.0.0-alpha.1 is out for testing, but an alpha is no foundation for a production theme, so everything below uses 5.3.

Decide whether Bootstrap belongs in the theme

Bootstrap fits a classic or hybrid theme whose templates you write in PHP, where you control the markup and can put row, col-md-6 or btn classes exactly where they belong. It also fits teams that already know its classes and want a consistent component set across several projects.

It fits poorly in a block theme. There, theme.json already defines the layout widths, spacing scale, color palette and typography, and core blocks print their own markup and classes. Adding Bootstrap means shipping a second layout system, and its Reboot stylesheet restyles elements that block styles already handle. If the site is edited in the Site Editor, stay with theme.json.

Start from a child theme

If you build on an existing classic theme, put your changes in a child theme, so an update to the parent does not erase them. The child needs a folder in wp-content/themes/ and a style.css whose Template line matches the parent's folder name:

/*
Theme Name: Example Bootstrap Child
Template: parent-theme-folder
Version: 1.0.0
*/

Ship Bootstrap from your own server

Download the compiled 5.3.9 files, bootstrap.min.css and bootstrap.bundle.min.js, into the child theme's assets folder. Serving them yourself avoids a request to a third-party CDN that would see every visitor's IP address, removes a DNS lookup and TLS connection, and keeps the files locked to the version you tested. Enqueue them in the child theme's functions.php:

<?php
add_action( 'wp_enqueue_scripts', function () {
    $uri = get_stylesheet_directory_uri();

    wp_enqueue_style( 'bootstrap', $uri . '/assets/css/bootstrap.min.css', array(), '5.3.9' );
    wp_enqueue_style( 'child-style', get_stylesheet_uri(), array( 'bootstrap' ), wp_get_theme()->get( 'Version' ) );

    wp_enqueue_script(
        'bootstrap',
        $uri . '/assets/js/bootstrap.bundle.min.js',
        array(),
        '5.3.9',
        array( 'strategy' => 'defer', 'in_footer' => true )
    );
} );

What each detail does:

  • The bundle includes Popper, which dropdowns, popovers and tooltips need.
  • strategy set to defer needs WordPress 6.3 or later, the release that turned the old in_footer boolean into an array of options. The script downloads in parallel and runs after the page is parsed.
  • The dependency on bootstrap loads your own stylesheet after Bootstrap, so your rules win ties. If the parent theme does not load its own stylesheet, enqueue it as well, between the two.
  • The version numbers change the query string, so browsers fetch new files after an upgrade.
  • wp_enqueue_scripts only fires on the front end. Admin screens use a different hook, so the dashboard never loads Bootstrap.

If you load it from a CDN anyway

WordPress prints no integrity attributes, so add them with the script_loader_tag filter, and its style_loader_tag twin for the stylesheet. Copy the hash from the Bootstrap documentation for the exact version you load, since every release has its own:

add_filter( 'script_loader_tag', function ( $tag, $handle ) {
    if ( 'bootstrap' === $handle ) {
        $tag = str_replace( ' src=', ' integrity="sha384-COPY-FROM-THE-BOOTSTRAP-DOCS" crossorigin="anonymous" src=', $tag );
    }
    return $tag;
}, 10, 2 );

Make the block editor resemble the front end

Even with a classic theme, editors usually write in the block editor, which does not load front-end styles. To preview your typography and colors there, opt in to editor styles:

add_action( 'after_setup_theme', function () {
    add_theme_support( 'editor-styles' );
    add_editor_style( 'assets/css/editor.css' );
} );

WordPress rewrites some selectors in that file, such as body, so the styles stay inside the editing canvas. Build editor.css with your fonts, colors and spacing rather than all of Bootstrap: the editor needs to look like the site, not run its components.

Give menus Bootstrap's classes

WordPress prints menus as a list with its own classes, while a Bootstrap navbar expects navbar-nav on the list, nav-item on each item and nav-link on each link. For a one-level menu, call wp_nav_menu() inside the navbar's collapse element with 'menu_class' => 'navbar-nav ms-auto', 'container' => false, 'depth' => 1 and 'fallback_cb' => false, then add the item and link classes with two filters instead of a custom walker:

add_filter( 'nav_menu_css_class', function ( $classes, $item, $args ) {
    if ( isset( $args->theme_location ) && 'primary' === $args->theme_location ) {
        $classes[] = 'nav-item';
    }
    return $classes;
}, 10, 3 );

add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
    if ( isset( $args->theme_location ) && 'primary' === $args->theme_location ) {
        $atts['class'] = 'nav-link' . ( $item->current ? ' active' : '' );
    }
    return $atts;
}, 10, 3 );

WordPress already adds aria-current="page" to the current item's link. Dropdown submenus need more: the nav_menu_submenu_css_class filter can add dropdown-menu to nested lists, but parent links also need toggle attributes, which is where a walker class usually comes in. The long-standing wp-bootstrap-navwalker project was written for Bootstrap 4: its README points to the v4.3.0 tag while the main branch is reworked, and Bootstrap 5 needs an extra filter to rename its data-toggle attributes to data-bs-toggle. Before adding one, ask whether a flat menu serves visitors on touch screens better.

Watch for three conflicts

  • The toolbar. A fixed-top navbar slides under the WordPress toolbar that logged-in users see. Offset it by the toolbar's height, 32 px on wide screens and 46 px at 782 px and below, with the rules shown after this list.
  • Shared class names. Plugins that print their own .container, .row or .btn markup pick up Bootstrap's styles too. Check forms, sliders and shop pages once Bootstrap is on.
  • Core markup. Comment forms, search forms and widgets print WordPress classes, not Bootstrap's. Filters cover most of it: comment_form_defaults, for example, can set class_submit to btn btn-primary.
body.admin-bar .fixed-top { top: 32px; }

@media screen and (max-width: 782px) {
    body.admin-bar .fixed-top { top: 46px; }
}

Ship only the parts you use

The full stylesheet carries every component. A Sass build includes only what the theme needs. Install Bootstrap and Dart Sass in the child theme:

cd wp-content/themes/example-bootstrap-child
npm init -y
npm install bootstrap@5.3.9 sass

Then list the parts in scss/theme.scss, in the order the Bootstrap documentation gives:

@import "bootstrap/scss/functions";
$primary: #0b5fff;
@import "bootstrap/scss/variables";
@import "bootstrap/scss/variables-dark";
@import "bootstrap/scss/maps";
@import "bootstrap/scss/mixins";
@import "bootstrap/scss/root";
@import "bootstrap/scss/utilities";
@import "bootstrap/scss/reboot";
@import "bootstrap/scss/type";
@import "bootstrap/scss/containers";
@import "bootstrap/scss/grid";
@import "bootstrap/scss/nav";
@import "bootstrap/scss/navbar";
@import "bootstrap/scss/transitions";
@import "bootstrap/scss/utilities/api";

Variable overrides such as $primary go after functions and before variables. Compile over the file you enqueue:

npx sass --load-path=node_modules --style=compressed --no-source-map --quiet-deps \
  scss/theme.scss assets/css/bootstrap.min.css

Recent Dart Sass versions print deprecation notices for Bootstrap's @import rules. Bootstrap's documentation says they do not affect the output and can be ignored while a long-term fix is worked out.

For icons, Bootstrap Icons 1.13.2 offers over 2,000 icons through npm install bootstrap-icons. Copy the few SVG files you need into the theme and inline them, instead of loading the icon font for a handful of glyphs.

To try all this without risking a live site, a WordPress one-click app on a small CloudSat server makes a staging copy where theme changes can break safely.

Other routes

  • A block theme with theme.json, for sites edited mainly in the Site Editor.
  • Tailwind CSS, now at version 4.3, for teams that prefer utility classes generated from the classes used in their templates.
  • Plain modern CSS, with grid, custom properties and container queries, which covers small themes without any framework.

FAQ

Should a new theme use Bootstrap 6?

Not yet. 6.0.0-alpha.1 is an early test release whose details may change. Build on 5.3.9 and try 6 on a separate branch.

Does Bootstrap slow WordPress down?

It adds bytes to every page, and the full stylesheet is mostly unused on a typical site. A Sass build of the parts you use and a deferred script keep the cost small. Check the browser's network panel that Bootstrap loads only once: some plugins bring their own copy.

Why do my Bootstrap dropdowns not open?

Usually one of three things: Bootstrap 4 markup with data-toggle instead of data-bs-toggle, the plain bootstrap.min.js without Popper where the bundle was needed, or two copies of Bootstrap's script on the same page.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.