WP BOX WordPress theme includes pipelines for compilation and dependency enqueuing preconfigured to work on specific directories in the project.
dependencies.json
that can be added into block directory. (Load a library only when its used by the block). "scripts": {
"start-tailwind": "npx tailwindcss -i ./styles/src/tailwind.css -o ./styles/dist/tailwind.css --watch=always",
"lint-js": "npx eslint .",
"format-js": "npx eslint --fix .",
"gulp": "gulp",
"start-components": "webpack --watch --config ./webpack/webpack.components.js",
"start-gutenberg-variations": "webpack --watch --config ./webpack/webpack.gutenberg-variations.js",
"start-gutenberg-hooks": "webpack --watch --config ./webpack/webpack.gutenberg-hooks.js",
"start-gutenberg-scripts": "webpack --watch --config ./webpack/webpack.gutenberg-scripts.js",
"start-gutenberg": "wp-scripts start --config ./webpack/webpack.gutenberg.js",
"start-svg": "wp-scripts start --config ./webpack/webpack.global-svg.js",
"start-imgs": "wp-scripts start --config ./webpack/webpack.global-images.js",
"start": "npm-run-all --parallel start-tailwind start-gutenberg start-components start-gutenberg-scripts start-gutenberg-hooks start-gutenberg-variations gulp"
},
WP BOX comes with Tailwind CSS integrated into frontend development flow.
const tailwindConfig = {
theme: {
extend: {
screens: {
sm: '480px',
md: '800px',
lg: '1024px',
xl: '1600px',
},
textColor: {
theme: {
base: 'var(--color-text-base)',
link: 'var(--color-text-link)',
hover: 'var(--color-text-hover)',
},
},
backgroundColor: {
theme: {
'light-gray': 'var(--color-bg-light-gray)',
'light-blue': 'var(--color-bg-light-blue)',
white: 'var(--color-bg-white)',
modal: 'var(--color-bg-modal)',
'bg-hover': 'var(--color-bg-hover)',
},
},
Variables defined in tailwind.config.js
are compiled into CSS classes together with default Tailwind utility classes, that are used in projects.
By default WP BOX withdraws post authors access to styling settings, to have a persistent style across website. Also this allows developers to build new components faster, reusing existing defines.
WP BOX provides a class, that merges Tailwind Config settings into theme.json
, default site does not use it, because editors access to theme styles is disabled, but in case you need it – you can pass the desired Tailwind settings into WordPress settings with a setSettings()
method of a WPThemeConfigurator class, which are then saved into a file.
Right now we just build a minimal empty theme.json
file, that is required to enable Full Site Editing feature of WordPress.
class WPThemeConfigurator {
constructor() {
this.$schema = 'https://schemas.wp.org/trunk/theme.json';
this.version = 3;
this.settings = {};
}
setSettings( config ) {
this.settings = { ...this.settings, ...config };
return this;
}
// Method to save configuration to theme.json file.
saveToThemeJSON( filePath = 'theme.json' ) {
const jsonString = JSON.stringify( this, null, 2 );
fs.writeFileSync( filePath, jsonString );
// eslint-disable-next-line no-console
console.log( `Configuration saved to ${ filePath }` );
}
}
If you ever wondered, how could one remember all those utility classes – the answer is you shouldn’t remember them.
Modern IDEs have extensions, that suggest autocompletion and provide explanation on Tailwind utility classes. Also these classes are well documented on the official website.
To have an even more reusable component system, WP BOX is utilizing Tailwind layers to create custom components. When is this needed?
Imagine that you want to have visually similar buttons across all website, but under the hood, they have different functionality. E.g. a button which is a link, a button which triggers ajax request and a button that runs some other frontend script. In this case, you would probably have 3 different Gutenberg blocks – button-link, button-pay and button-script.
To avoid copy-pasting of utility classes in every block, you can define them once in a components layer, and reuse in all your components.
@layer components {
.wpbox-input {
@apply relative;
input {
@apply bg-theme-white text-theme-base w-full pt-6 px-4 pb-2 rounded outline-none;
}
label {
@apply absolute transition-all start-4 top-4 opacity-60 text-base-paragraph md:text-base-paragraph-desktop pointer-events-none;
}
}
.wpbox-button {
@apply block whitespace-nowrap max-w-fit cursor-pointer text-theme-link hover:text-theme-hover border-2 rounded-md border-theme-link px-4 py-3.5 no-underline hover:bg-theme-bg-hover;
}
}
public static function theme_gutenberg_blocks()
{
$blocks_parent_directory = get_template_directory() . '/blocks/';
// Scan and register blocks from the parent theme directory
self::register_blocks($blocks_parent_directory, false);
if (is_child_theme()) {
$blocks_child_directory = get_stylesheet_directory() . '/blocks/';
// Scan and register blocks from the child theme directory
self::register_blocks($blocks_child_directory, true);
}
}
The blocks
directory in the theme folder contains all resources related to blocks, and the ThemeBlocks
class is responsible for registering blocks and loading their assets.
To add new blocks, you need to follow the naming and file structuring rules. Files such as block.json
, render.php
and block.index.js
placed in the block’s directory, as well as resources in the ./assets/
folder, will be registered, loaded, and available for use.
Theme features another solution for simple and unified implementation of overrides, variations and patterns. Backend classes ThemeBlocksOverrides
, ThemeBlocksVariations
and ThemeBlocksPatterns
are responsible for scanning respective directories and implementing hooks.js
and filters.php
defined in the folder during the initialization.
Note the screenshot with the example on how the core/code
block is overriden, which you saw on this page.
The project includes several dozen ready-made blocks and overrides of core blocks that you can learn from to create your custom blocks, use them as is, or modify to your needs:
As you probably already noticed above in the pipelines section, WP BOX includes code style assisting implemented with ESLint packages and WordPress code style configuration with few changes. Also you will need to setup your IDE to to use project config.
Save time, money and nerves by investing in awesome code base early and be prepared when your project needs it.
50% discount applies on purchases in December 2024/January 2025. Happy New Year!
Visit pricing details page