WordPress development has evolved significantly, and modern tooling plays a crucial role in creating efficient and maintainable projects. wp-scripts
is a powerful package that simplifies the development process, providing a standardized and streamlined way to build themes and plugins utilizing modern JavaScript, CSS, and other front-end technologies. This comprehensive guide will walk you through how to leverage wp-scripts
for your next WordPress project.
What is wp-scripts
?
wp-scripts
is a zero-configuration build tool built specifically for WordPress development. It’s included with the @wordpress/scripts
npm package and aims to provide a consistent and reliable development experience by handling common tasks like:
Why Use wp-scripts
?
wp-scripts
provides sensible defaults for common WordPress development scenarios.Setting Up a Project with wp-scripts
mkdir my-wp-project cd my-wp-project
Package Initialization: Initialize a new package.json
file using npm.
npm init -y
Install @wordpress/scripts
: Install the @wordpress/scripts
package as a development dependency.
npm install @wordpress/scripts --save-dev
Configure package.json
: Add scripts to your package.json
file to run various wp-scripts
commands. The most common scripts are start
, build
, and lint
.
{ "name": "my-wp-project", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "wp-scripts start", "build": "wp-scripts build", "lint:js": "wp-scripts lint-js", "lint:css": "wp-scripts lint-style", "packages-update": "wp-scripts packages-update" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@wordpress/scripts": "^26.3.0" } }
start
: Starts the development server and watches for file changes.build
: Creates production-ready bundles of your code.lint:js
: Lints JavaScript files using ESLint.lint:css
: Lints CSS files using Stylelint.packages-update
: Updates all WordPress packages to the latest version.Project Structure
While wp-scripts
doesn’t enforce a strict project structure, a recommended approach is:
my-wp-project/ ├── src/ # Source files │ ├── index.js # Entry point for JavaScript │ ├── style.scss # Main stylesheet │ └── blocks/ # Directory for custom blocks (if applicable) ├── dist/ # Compiled output (automatically generated) ├── package.json ├── package-lock.json ├── node_modules/ # Dependencies (automatically generated) └── .eslintrc.js # ESLint configuration (optional)
Basic Usage: Styling
Let’s create a simple stylesheet.
src/style.scss
: Create a new file at src/style.scss
and add some basic CSS.body { font-family: sans-serif; background-color: #f0f0f0; padding: 20px; } h1 { color: #333; }
Enqueue the Stylesheet: In your WordPress theme’s functions.php
file (or plugin main file), enqueue the compiled CSS file.
<?php function my_wp_project_enqueue_assets() { wp_enqueue_style( 'my-wp-project-style', get_stylesheet_directory_uri() . '/dist/style.css', // Adjust to your theme directory if necessary array(), filemtime( get_stylesheet_directory() . '/dist/style.css' ) // Cache busting ); } add_action( 'wp_enqueue_scripts', 'my_wp_project_enqueue_assets' );
npm start
or npm run build
: Execute npm start
in your terminal to start the development server with hot-reloading. wp-scripts
will automatically compile your scss
file to dist/style.css
. Alternatively, use npm run build
to create a production-ready version.Basic Usage: JavaScript
src/index.js
: Create a new file at src/index.js
and add some JavaScript code.console.log('Hello from wp-scripts!'); document.addEventListener('DOMContentLoaded', function() { const heading = document.querySelector('h1'); if (heading) { heading.textContent = 'Hello World!'; } });
2. Enqueue the Script: Enqueue the compiled JavaScript file in your WordPress theme’s functions.php
file (or plugin main file).
<?php function my_wp_project_enqueue_assets() { wp_enqueue_script( 'my-wp-project-script', get_stylesheet_directory_uri() . '/dist/index.js', // Adjust to your theme directory if necessary array(), filemtime( get_stylesheet_directory() . '/dist/index.js' ), // Cache busting true // Place script in the footer ); } add_action( 'wp_enqueue_scripts', 'my_wp_project_enqueue_assets' );
npm start
or npm run build
: Just like with the styling, execute npm start
to start the development server or npm run build
to create a production-ready bundle. wp-scripts
will compile your JavaScript code, bundle it, and place the output in dist/index.js
.Working with Blocks
wp-scripts
is particularly well-suited for developing custom Gutenberg blocks. Here’s how to set up a basic block:
src
directory, create a new directory for your blocks, e.g., src/blocks/
.src/blocks/
directory, create two files for your block: block.js
(for JavaScript) and block.scss
(for CSS). src/blocks/block.js
:import { registerBlockType } from '@wordpress/blocks'; import './block.scss'; registerBlockType( 'my-wp-project/my-custom-block', { title: 'My Custom Block', icon: 'smiley', category: 'common', edit: () => { return <p>Hello from the editor!</p>; }, save: () => { return <p>Hello from the frontend!</p>; }, } );
2. src/blocks/block.scss
:
.wp-block-my-wp-project-my-custom-block { background-color: lightblue; padding: 10px; }
3. Import Block in index.js
: Import your block into src/index.js
to register it.
import './blocks/block.js'; // Import your block console.log('Hello from wp-scripts!'); document.addEventListener('DOMContentLoaded', function() { const heading = document.querySelector('h1'); if (heading) { heading.textContent = 'Hello World!'; } });
npm start
or npm run build
: Running npm start
or npm run build
will now automatically bundle your block code, making it available in the Gutenberg editor.Advanced Configuration (Optional)
While wp-scripts
aims for zero configuration, you can customize its behavior by creating configuration files in your project root:
wp-scripts eject
, this will give you the full webpack configuration.Example: Customizing ESLint
.eslintrc.js
: Create a file named .eslintrc.js
in your project root.console.log
statements:module.exports = { "extends": "@wordpress/eslint-plugin", "rules": { "no-console": "off" // Disable the no-console rule } };
Common Issues and Troubleshooting
@wordpress/scripts
as a development dependency (npm install @wordpress/scripts --save-dev
) and that your node_modules
directory is present.npm install module-name
).functions.php
file and that the file path is correct. Also, check your browser’s cache.wp-scripts
commands outside of the project root directory. Ensure you’re in the directory containing package.json
.Conclusion
wp-scripts
offers a streamlined and efficient approach to modern WordPress development. By leveraging its zero-configuration design and powerful tooling, you can focus on building innovative and high-quality themes and plugins without the complexities of managing intricate build processes. By following this guide, you can start building your next WordPress project with confidence using wp-scripts
. Remember to consult the official @wordpress/scripts
documentation for the most up-to-date information and advanced configuration options.
This guide provides a detailed walkthrough of installing and configuring an Apache web server and…
I. Project Overview The goal is to automate the process of notifying search engines (like…
1. Database Structure (MySQL) We'll need a database table to store information about our website's…
This explanation aims to provide a solid foundation for understanding the process and implementing your…
Okay, here's a comprehensive guide on building a real-time website chat script using PHP, HTML,…
Comprehensive guide on creating a simple website analytics system using PHP, HTML, CSS, JavaScript, and…