Want to create a plugin to extend WooCommerce? You’re in the right place.
WooCommerce extensions are the same as regular WordPress plugins. For more information, visit Writing a plugin.
Your WooCommerce extension should:
- Adhere to all WordPress plugin coding standards, as well as best practice guidelines for harmonious existence within WordPress and alongside other WordPress plugins.
- Have a single core purpose and use WooCommerce features as much as possible.
- Adhere to WooCommerce compatibility and interoperability guidelines.
Merchants make use of WooCommerce extensions daily, and should have a unified and pleasant experience while doing so without advertising invading their WP Admin or store.
- Check if WooCommerce is active. Most WooCommerce plugins do not need to run unless WooCommerce is already active. Learn how to check if WooCommerce is active.
The main plugin file should adopt the name of the plugin. For example: A plugin with the directory name
plugin-namewould have its main file named
The text domain should match your plugin directory name. For example: A plugin with a directory name of
plugin-namewould have the text domain
plugin-name. Do not use underscores.
- Internationalization: Follow guidelines for Internationalization for WordPress Developers
- Localization: All text strings within the plugin code should be in English. This is the WordPress default locale, and English should always be the first language. If your plugin is intended for a specific market (e.g., Spain or Italy), include appropriate translation files for those languages within your plugin package. Learn more at Using Makepot to translate your plugin.
- Follow WordPress PHP Guidelines. WordPress has a set of guidelines to keep all WordPress code consistent and easy to read. This includes quotes, indentation, brace style, shorthand php tags, yoda conditions, naming conventions, and more. Please review the guidelines.
- Avoid creating custom database tables. Whenever possible, use WordPress post types, taxonomies, and options. For more, check out our primer on data storage.
- Prevent Data Leaks by ensuring you aren’t providing direct access to PHP files. Find out how.
- All plugins need a standard WordPress README. See an example in the WordPress plugin README file standard.
Follow our conventions for your Plugin header comment. View our example WordPress plugin header comment and follow the conventions listed, including:
WC requires at least:and
WC tested up to:, and
- Make it extensible: use WordPress actions and filters to allow for modification/customization, and if your plugin creates a front-end output, we recommend having a templating engine in place so users can create custom template files in their theme’s WooCommerce folder to overwrite the plugin’s template files.For more information, check out Pippin’s post on Writing Extensible Plugins with Actions and Filters.
- Avoid external libraries. The use of entire external libraries is typically not suggested as this can open up the product to security vulnerabilities. If an external library is absolutely necessary, developers should be thoughtful about the code used and assume ownership as well as of responsibility for it. Try to only include the strictly necessary part of the library, or use a WordPress-friendly version or opt to build your own version. For example, if needing to use a text editor such as TinyMCE, we recommend using the WordPress-friendly version, TinyMCE Advanced.
- Remove unused code. With version control, there’s no reason to leave commented-out code; it’s annoying to scroll through and read. Remove it and add it back later if needed.
- Use Comments to describe the functions of your code. If you have a function, what does the function do? There should be comments for most if not all functions in your code. Someone/You may want to modify the plugin, and comments are helpful for that. We recommend using PHP Doc Blocks similar to WooCommerce.
- Avoid God Objects. God Objects are objects that know or do too much. The point of object-oriented programming is to take a large problem and break it into smaller parts. When functions do too much, it’s hard to follow their logic, making bugs harder to fix. Instead of having massive functions, break them down into smaller pieces.
- Test Your Code with WP_DEBUG mode on, so you can see all PHP warnings sent to the screen. This will flag things like making sure a variable is set before checking the value.
- Separate Business Logic & Presentation Logic. It’s a good practice to separate business logic (i.e., how the plugin works) from presentation logic (i.e., how it looks). Two separate pieces of logic are more easily maintained and swapped if necessary. An example is to have two different classes – one for displaying the end results, and one for the admin settings page.
- Use Transients to Store Offsite Information. If you provide a service via an API, it’s best to store that information so future queries can be done faster and the load on your service is lessened. WordPress transients can be used to store data for a certain amount of time.
- Log data that can be useful for debugging purposes, with two conditions: Allow any logging as an ‘opt in’, and Use the WC_Logger class. A user can then view logs on their system status page. Learn how to add a link to logged data in your extension.
Last updated: February 02, 2024