This plugin is a template for creating a WordPress plugin. It is a fully working plugin itself, but it has very limited functionality. It only includes a starting point for features that you may want to implement in your own plugin. Remove features that you will not need and then extend the features you keep or add your own.
The goal is to provide a reliable, portable codebase with well-defined dependencies and minimal time for someone to make their first contribution even if they have never seen this code before.
Note: if using git
with SSH credentials, you may need to use an SSH style URL (git@github.com:zachwatkins/wordpress-plugin-name.git) instead of HTTPS (https://github.com/zachwatkins/wordpress-plugin-name.git).
Table of Contents
- Developer Installation
- Creating a New Plugin
- Directory Structure
- Commands
- Tests
- Installing System Requirements for Development
- Further Reading
Run the commands below and then open your browser to http://localhost:8888. Your username and password are admin -> password
.
git clone https://github.com/zachwatkins/wordpress-plugin-name
cd your-plugin-name
npm install
npm start
cd ~/Local Sites/your-site/app/public/wp-content/plugins
git clone https://github.com/zachwatkins/wordpress-plugin-name
cd wordpress-plugin-name
npm install
composer install
If you want static code analysis for WordPress coding standards, install PHPCS and the WordPress coding standards globally:
composer global require squizlabs/php_codesniffer --dev
composer global require dealerdirect/phpcodesniffer-composer-installer --dev
composer global require wp-coding-standards/wpcs --dev
Next, run 'phpcs' from the root directory of this project to evaluate your plugin's source code and report any errors or warnings detected. This tool uses the WordPress coding standards and any rules defined in your phpcs.xml.dist file.
If the 'phpcs' command is not found, then make sure you have the composer bin dir in your PATH. The default value is ~/.config/composer/vendor/bin/, but you can check the value that you need to use by running composer global config bin-dir --absolute
.
Note: You can remove this section from your new plugin's documentation.
To create your own plugin using this template, choose one of the approaches below. If you have not done so already, rename git's default branch name: git config --global init.defaultBranch main
git clone https://github.com/zachwatkins/wordpress-plugin-name your-plugin-name
cd your-plugin-name
rm -rf .git
npm run template
git init
git add .
git commit -m "initial commit"
git branch -M main
git remote add origin https://github.com/ttitamu/your-plugin-name.git
git push -u origin main
npm install
npm start
cd ~/Local Sites/your-site/app/public/wp-content/plugins
git clone https://github.com/zachwatkins/wordpress-plugin-name your-plugin-name
cd your-plugin-name
rm -rf .git
npm run template
git init
git add .
git commit -m "initial commit"
git branch -M main
git remote add origin https://github.com/ttitamu/your-plugin-name.git
git push -u origin main
npm install
composer install
- .bin - Custom scripts for local development.
- .config - Configuration files for development tools used in this project.
- .github - GitHub integration files such as Actions workflows.
- .vscode - Visual Studio Code integration files.
- .wp-env - WordPress development environment default content.
- config - Configuration file for a settings page feature.
- docs - Documentation files going in depth on different aspects of this project or WordPress development.
- library - A library of common WordPress feature implementations that I'm developing alongside this template plugin. Do not modify them - treat this directory as an external library.
- src - Source code for the plugin's features, aside from the settings page feature and the library.
- src/admin - Source code for features specific to the admin user interface.
- src/admin/views - Files which render HTML for the admin user interface.
- src/assets - Non-PHP files used by the plugin, such as JavaScript, CSS, and images.
- src/views - Files which render HTML for site visitors.
- src/services - Interfaces between this plugin and external services.
- src/demo.php - Inserts demo content into your website to demonstrate your plugin's features during development.
- test - Plugin code tests.
a. e2e - Browser tests using Playwright.
b. jest - JavaScript tests using Jest.
c. phpunit - WordPress PHP code tests using PHPUnit. - index.php - The entrypoint for your plugin. This file is loaded by WordPress when the plugin is active for a site.
The commands you will use the most frequently for developing a plugin with this repository are listed below.
For a complete list of commands, refer to package.json and composer.json. For descriptions of what these commands do, see here: docs/commands.md
|-------------------------------------------------------------------------------------|
Command | Description |
---|---|
npm install |
Install your project dependencies for the first time. |
npm start |
Start the development environment |
npm run lint |
Check JS and CSS code style using WordPress coding standards |
npm run lint:php |
Check PHP code style using WordPress coding standards |
npm run test |
Test JavaScript and PHP |
npm run stop |
Stop the development environment |
------------------------------------------------------------------------------------- |
This plugin has a small set of tests to show you how to create your own. Core WordPress code is tested, so only test the code you write.
Categories of test included in this theme:
- Unit tests examine the behavior of a small unit of code.
- End to end tests examine what the end user sees.
- Integration tests examine compatibility between separate systems.
Ensure that Docker is running, then:
$ cd /path/to/a/wordpress/plugin-or-theme
$ npm -g i @wordpress/env
$ wp-env start
The local environment will be available at http://localhost:8888 (Username: admin
, Password: password
).
The database credentials are: user root
, password password
. For a comprehensive guide on connecting directly to the database, refer to Accessing the MySQL Database.
For documentation on .wp-env.json options see here: https://github.com/WordPress/gutenberg/tree/trunk/packages/env#wp-envjson
You will need the following tools installed on your computer:
To make this easier, you can use an installer included in this repository by saving it to your computer and making it executable. You must have administrator rights to run these installers.
- Copy this file to your computer: .bin/install/mac.sh
- Open your terminal and navigate to the directory where you saved the file.
- Make the file executable:
chmod +x mac.sh
- Run the file:
./mac.sh
- Copy this file to your computer: .bin/install/windows.bat
- Open your terminal and navigate to the directory where you saved the file.
- Run the file:
windows.bat
The links below describe important WordPress code concepts you may need to know when developing your WordPress plugin.