How to Scaffold a Plugin with WP-CLI and Set Up Testing

This article is focused on the practical implementation of WordPress plugin testing.

With WP-CLI we can generate a production-ready plugin structure that include comprehensive testing infrastructure from the start. We’ll walk through the process to get a professional WordPress plugin structure with proper testing setup.

By the end of this guide, you’ll understand how to:

  • Generate plugins using WP-CLI’s scaffold command with complete testing infrastructure
  • Set up WordPress test suites and databases for integration testing

Table of Contents

  1. Scaffold a plugin with WP-CLI
    1. Navigate to plugins directory
    2. Generate the plugin
    3. What gets generated
    4. Verify plugin creation
  2. Add test environment (installer)
    1. mysql and mysqladmin client tools
    2. svn tool
    3. Running install-wp-tests.sh
  3. Conclusion

Scaffold a plugin with WP-CLI

Instead of manually creating files and headers, let WP-CLI’s scaffold command generate a clean, standards-friendly starter. The scaffold includes your main plugin file, a readme, editor/git config, and (by default) a full PHPUnit test skeleton and CI config, so you can start coding and testing immediately.

cd wp-content/plugins

Generate the plugin

wp scaffold plugin my-awesome-plugin \
  --plugin_name="My Awesome Plugin" \
  --plugin_description="Short description of what it does." \
  --plugin_author="Your Name" \
  --plugin_author_uri="https://example.com" \
  --plugin_uri="https://example.com/my-awesome-plugin" \
  --activate \
  --ci=github

Scaffold options explained:

  • --activate — immediately activates the plugin after creation
  • --ci=github — generates GitHub Actions workflow (supports circle, gitlab, bitbucket)
  • --skip-tests — omit this flag to include PHPUnit test scaffolding

What gets generated

Core plugin files (always created):

  • my-awesome-plugin.php — main plugin file with proper headers and activation/deactivation hooks
  • readme.txtWordPress.org-style readme with installation instructions
  • .editorconfig — consistent coding standards across editors
  • .gitignore — excludes vendor/, node_modules/, and build files
  • .distignore — files excluded from distribution zips (tests, dev dependencies)

Development workflow files:

  • package.json — NPM scripts for start, readme, i18n tasks
  • Gruntfile.js — automated tasks for internationalization and documentation

Testing infrastructure (included by default):

  • phpunit.xml.dist — PHPUnit configuration for integration tests
  • bin/install-wp-tests.sh — script to download WordPress test suite and create test database
  • tests/bootstrap.php — loads WordPress testing environment and your plugin
  • tests/test-sample.php — example test case to get you started
  • .phpcs.xml.distPHP CodeSniffer rules for WordPress coding standards

CI/CD configuration:

  • .github/workflows/ — GitHub Actions workflow (when using --ci=github)
  • Supports CircleCI, GitLab CI, and Bitbucket Pipelines

Verify plugin creation

  • Check http://localhost:8888/mysite/wp-admin/plugins.php — your plugin should be active
  • Explore the generated files: ls -la my-awesome-plugin/
  • Enter into the plugin’s folder cd wp-content/plugins/my-awesome-plugin

Add test environment (installer)

Now we need to install the WordPress test suite and create the test DB with the provided script. We can do that by running the script bin/install-wp-tests.sh.

Before running the installer, remember bin/install-wp-tests.sh needs the MySQL client tools (mysql, mysqladmin). On macOS with MAMP they live here:

mysql and mysqladmin client tools

With MAMP these MySQL client tools are available in several folders (depending on the MySQL version):

  • MySQL 5.7 → /Applications/MAMP/Library/bin/mysql57/bin/
  • MySQL 8.0 → /Applications/MAMP/Library/bin/mysql80/bin/

Add the right folder to your PATH (as I use zsh, in the ~/.zshrc file on my case ), then reload:

# Pick one:
echo 'export PATH="/Applications/MAMP/Library/bin/mysql80/bin:$PATH"' >> ~/.zshrc
# echo 'export PATH="/Applications/MAMP/Library/bin/mysql57/bin:$PATH"' >> ~/.zshrc

source ~/.zshrc
which mysql && mysql --version

svn tool

bin/install-wp-tests.sh also needs svn client tool. In Mac it can be installed with homebrew

brew install subversion

Running install-wp-tests.sh

With mysql, mysqladmin and svn available from the terminal we can now run the installer to create the wordpress_test DB to fetch the WP test suite:

# This script downloads the WordPress testing suite from the official repository
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:8889 latest

The bin/install-wp-tests.sh script sets up WordPress testing infrastructure by performing three key tasks: creating a test database, downloading WordPress core files, and installing the official WordPress test suite.

The script downloads content from two official WordPress repositories and creates this structure at either the path at the environment variable $WP_TESTS_DIR or at $TMPDIR/wordpress*:

This setup enables integration testing where your plugin can interact with actual WordPress APIs, hooks, and database functions rather than just mocked versions.

Conclusion

Using WP-CLI’s scaffold command gives you a complete plugin testing setup without the manual configuration. This approach creates professional plugin structures that include all the testing infrastructure from day one.

You now have a WordPress plugin development environment (including a DB for tests) that supports PHPUnit testing.

Leave a Reply

Navigation

About

Writing on the Wall is a newsletter for freelance writers seeking inspiration, advice, and support on their creative journey.

Discover more from JuanMa Codes

Subscribe now to keep reading and get access to the full archive.

Continue reading